Skip to content

Commit

Permalink
Merge pull request #22 from shweshi/dev
Browse files Browse the repository at this point in the history
Added support to fetch all metadata
  • Loading branch information
shweshi authored Jan 4, 2019
2 parents 9df4204 + 4ac87b6 commit e08d5a9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `OpenGraph` will be documented in this file

## 1.0.3 - 2019-01-04
- Added support to fetch all metadata

## 1.0.2 - 2018-11-23
- Added tests and autoload for laravel >= 5.5

Expand Down
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of
use OpenGraph;
$data = OpenGraph::fetch("https://unsplash.com/");
```

this will give you an array like this..

```
Expand All @@ -69,6 +67,42 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of
'image:secure_url' => 'https://images.unsplash.com/photo-1542841791-1925b02a2bbb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9&s=aceabe8a2fd1a273da24e68c21768de0',
)
```

You can also pass an optional parameter either true or false along with url. When set false it will only fetch basic metadata and in case of true it will fetch all the other optional metadata as well like audio, video, music and twitter metatags as well.

```
$data = OpenGraph::fetch("https://unsplash.com/", true);
```

this will give you an array like this..

```
array (
'charset' => 'UTF8',
'viewport' => 'width=device-width, initial-scale=1.0, maximum-scale=1.0, minimal-ui',
'mobile-web-app-capable' => 'yes',
'apple-mobile-web-app-capable' => 'yes',
'apple-mobile-web-app-title' => 'Unsplash',
'application-name' => 'Unsplash',
'author' => 'Unsplash',
'msapplication-config' => 'browserconfig.xml',
'msapplication-TileColor' => '#ffffff',
'msapplication-TileImage' => 'https://unsplash.com/mstile-144x144.png',
'theme-color' => '#ffffff',
'description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.',
'twitter:site' => '@unsplash',
'twitter:title' => 'Beautiful Free Images & Pictures | Unsplash',
'twitter:description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.',
'twitter:url' => 'https://unsplash.com/',
'twitter:card' => 'summary_large_image',
'twitter:image' => 'https://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9',
'title' => 'Beautiful Free Images & Pictures | Unsplash',
'type' => 'website',
'url' => 'https://unsplash.com/',
'image' => 'http://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9',
'image:secure_url' => 'https://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9',
)
```

## Testing

Expand All @@ -85,4 +119,4 @@ The MIT License (MIT). Please see [License File](LICENSE) for more information.

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph?ref=badge_large)

### Happy coding!
### Happy coding!
12 changes: 8 additions & 4 deletions src/OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class OpenGraph
{
public function fetch($url)
public function fetch($url, $allMeta = null)
{
$html = $this->curl_get_contents($url);

Expand All @@ -18,10 +18,14 @@ public function fetch($url)

$tags = $doc->getElementsByTagName('meta');
$metadata = [];

foreach ($tags as $tag) {
if ($tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
$metaproperty = ($tag->hasAttribute('property')) ? $tag->getAttribute('property') : $tag->getAttribute('name');
if (!$allMeta && $metaproperty && strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($metaproperty, 3), '-', '_');
$value = $tag->getAttribute('content');
}
if ($allMeta && $metaproperty) {
$key = (strpos($metaproperty, 'og:') === 0) ? strtr(substr($metaproperty, 3), '-', '_') : $metaproperty;
$value = $tag->getAttribute('content');
}
if (!empty($key)) {
Expand Down

0 comments on commit e08d5a9

Please sign in to comment.