From 8137872fd850869ac04d117175aff924480d07fd Mon Sep 17 00:00:00 2001 From: Aram Zucker-Scharff Date: Tue, 12 Jun 2012 21:19:28 -0300 Subject: [PATCH] Now can add title and description from head meta even if no Open Graph tags exist. --- OpenGraph.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/OpenGraph.php b/OpenGraph.php index 98adf5e..af26618 100644 --- a/OpenGraph.php +++ b/OpenGraph.php @@ -13,6 +13,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + + Original can be found at https://github.com/scottmac/opengraph/blob/master/OpenGraph.php + */ class OpenGraph implements Iterator @@ -72,6 +75,8 @@ static private function _parse($HTML) { $page = new self(); + $nonOgDescription = null; + foreach ($tags AS $tag) { if ($tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) { @@ -79,15 +84,28 @@ static private function _parse($HTML) { $page->_values[$key] = $tag->getAttribute('content'); } - //Added this if loop to retrieve description values from sites like the New York Times who have malformed it. if ($tag ->hasAttribute('value') && $tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) { $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_'); $page->_values[$key] = $tag->getAttribute('value'); } + //Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php + if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'description') { + $nonOgDescription = $tag->getAttribute('content'); + } } + //Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php + if (!isset($page->_values['title'])) { + $titles = $doc->getElementsByTagName('title'); + if ($titles->length > 0) { + $page->_values['title'] = $titles->item(0)->textContent; + } + } + if (!isset($page->_values['description']) && $nonOgDescription) { + $page->_values['description'] = $nonOgDescription; + } if (empty($page->_values)) { return false; } @@ -160,4 +178,4 @@ public function current() { return current($this->_values); } public function key() { return key($this->_values); } public function next() { next($this->_values); ++$this->_position; } public function valid() { return $this->_position < sizeof($this->_values); } -} +} \ No newline at end of file