From b792e09cee0d34424566b0cfff9a9d3686ac23c9 Mon Sep 17 00:00:00 2001 From: Shashi Prakash Gautam Date: Fri, 4 Jan 2019 18:43:09 +0530 Subject: [PATCH 1/7] Added support to fetch all metadata Added support to fetch all metadata by passing true/false. --- src/OpenGraph.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/OpenGraph.php b/src/OpenGraph.php index 9aae6ed..5256b5d 100644 --- a/src/OpenGraph.php +++ b/src/OpenGraph.php @@ -6,7 +6,7 @@ class OpenGraph { - public function fetch($url) + public function fetch($url, $allMeta = null) { $html = $this->curl_get_contents($url); @@ -18,17 +18,20 @@ 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)) { $metadata[$key] = $value; } } - return $metadata; } From 321b04afbeb73017a67f810507f58625aca8f7ec Mon Sep 17 00:00:00 2001 From: Shashi Prakash Gautam Date: Fri, 4 Jan 2019 18:44:09 +0530 Subject: [PATCH 2/7] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11baa44..bd087e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 2ab80e1acf5240bf0da3d1d1fe9ce194e5731ef0 Mon Sep 17 00:00:00 2001 From: Shashi Prakash Gautam Date: Fri, 4 Jan 2019 18:53:39 +0530 Subject: [PATCH 3/7] Update README.md --- README.md | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7eea890..4409c95 100644 --- a/README.md +++ b/README.md @@ -54,10 +54,14 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of use OpenGraph; $data = OpenGraph::fetch("https://unsplash.com/"); + + You can also pass an optional parameter either true or false. Which specify whether to fetch all metadata or only og metadata. By default its false. + + $data = OpenGraph::fetch("https://unsplash.com/", true); ``` - this will give you an array like this.. + this will give you an array like this if by default.. ``` array ( @@ -69,6 +73,36 @@ 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', ) ``` + + or with parameter set to true the response will be.. + + ``` + 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 @@ -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! \ No newline at end of file +### Happy coding! From c310438618de8e17d93bdb883c01b332ee8c5656 Mon Sep 17 00:00:00 2001 From: Shashi Prakash Gautam Date: Fri, 4 Jan 2019 18:54:57 +0530 Subject: [PATCH 4/7] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4409c95..7f35812 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,9 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of use OpenGraph; $data = OpenGraph::fetch("https://unsplash.com/"); - + ``` You can also pass an optional parameter either true or false. Which specify whether to fetch all metadata or only og metadata. By default its false. - + ``` $data = OpenGraph::fetch("https://unsplash.com/", true); ``` From cb6bc03b968895579f509f57d8eeb74f6aa9812a Mon Sep 17 00:00:00 2001 From: Shashi Prakash Gautam Date: Fri, 4 Jan 2019 18:58:37 +0530 Subject: [PATCH 5/7] Update README.md --- README.md | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 7f35812..a1ff9b8 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,10 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of $data = OpenGraph::fetch("https://unsplash.com/"); ``` - You can also pass an optional parameter either true or false. Which specify whether to fetch all metadata or only og metadata. By default its false. + You can also pass an optional parameter either true or false. 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 if by default.. @@ -78,29 +78,29 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of ``` 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', + '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', ) ``` From c7fcd681c1370d462658b30e7b0241283efa0cde Mon Sep 17 00:00:00 2001 From: Shashi Prakash Gautam Date: Fri, 4 Jan 2019 19:01:49 +0530 Subject: [PATCH 6/7] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a1ff9b8..86df391 100644 --- a/README.md +++ b/README.md @@ -55,13 +55,7 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of $data = OpenGraph::fetch("https://unsplash.com/"); ``` - You can also pass an optional parameter either true or false. 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 if by default.. + this will give you an array like this.. ``` array ( @@ -74,7 +68,13 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of ) ``` - or with parameter set to true the response will be.. + 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 ( From 71f03c114836ed4208c01c5896655d85aae16ba5 Mon Sep 17 00:00:00 2001 From: Shashi Prakash Gautam Date: Fri, 4 Jan 2019 13:31:54 +0000 Subject: [PATCH 7/7] Apply fixes from StyleCI --- src/OpenGraph.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenGraph.php b/src/OpenGraph.php index 5256b5d..cc7fa4c 100644 --- a/src/OpenGraph.php +++ b/src/OpenGraph.php @@ -32,6 +32,7 @@ public function fetch($url, $allMeta = null) $metadata[$key] = $value; } } + return $metadata; }