_server/opengraph.php
/**
   * Parses HTML and extracts Open Graph data, this assumes
   * the document is at least well formed.
   *
   * @param $HTML    HTML to parse
   * @param $URI    URI to page to parse for Open Graph data
   * @return OpenGraph
   */
Parses HTML and extracts Open Graph data, this assumes
the document is at least well formed.

@param $HTML    HTML to parse
@param $URI    URI to page to parse for Open Graph data
@return OpenGraph
Parses HTML and extracts Open Graph data this assumes the document is at least well formed . @param $HTML HTML to parse @param $URI URI to page to parse for Open Graph data @return OpenGraph
static private function _parse($HTML, $URI) {
		$old_libxml_error = libxml_use_internal_errors(true);
		$doc = new DOMDocument();
		$doc->loadHTML($HTML);
		libxml_use_internal_errors($old_libxml_error);
		$tags = $doc->getElementsByTagName('meta');
		if (!$tags || $tags->length === 0) {
			return false;
		}
		$page = new self();
		$nonOgDescription = null;
		foreach ($tags AS $tag) {
			if ($tag->hasAttribute('property') &&
			    strpos($tag->getAttribute('property'), 'og:') === 0) {
				$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
				$page->_values[$key] = $tag->getAttribute('content');
			}
			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');
			}
			if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'description') {
                $nonOgDescription = $tag->getAttribute('content');
            }
		}
		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 (!isset($page->_values['image'])) {
        $domxpath = new DOMXPath($doc);
        $elements = $domxpath->query("//link[@rel='image_src']");
        if ($elements->length > 0) {
            $domattr = $elements->item(0)->attributes->getNamedItem('href');
            if ($domattr) {
                $page->_values['image'] = $domattr->value;
                $page->_values['image_src'] = $domattr->value;
            }
        }
    }
    if (!isset($page->_values['image'])) {
        $domxpath = new DOMXPath($doc);
        $elements = $domxpath->query("//link[@rel='apple-touch-icon'][last()]");
        if ($elements->length > 0) {
            $domattr = $elements->item(0)->attributes->getNamedItem('href');
            if ($domattr) {
                $page->_values['image'] = $domattr->value;
                $page->_values['image_src'] = $domattr->value;
            }
        }
    }
		if (empty($page->_values)) { return false; }
    $page->_values['image'] = ltrim($page->_values['image'], '/');
		return $page;
	}
static private function _parse ( $ HTML , $ URI ) { $ old_libxml_error = libxml_use_internal_errors ( true ) ; $ doc = new DOMDocument ( ) ; $ doc -> loadHTML ( $ HTML ) ; libxml_use_internal_errors ( $ old_libxml_error ) ; $ tags = $ doc -> getElementsByTagName ( 'meta' ) ; if ( ! $ tags || $ tags -> length === 0 ) { return false ; } $ page = new self ( ) ; $ nonOgDescription = null ; foreach ( $ tags AS $ tag ) { if ( $ tag -> hasAttribute ( 'property' ) && strpos ( $ tag -> getAttribute ( 'property' ) , 'og:' ) === 0 ) { $ key = strtr ( substr ( $ tag -> getAttribute ( 'property' ) , 3 ) , '-' , '_' ) ; $ page -> _values [ $ key ] = $ tag -> getAttribute ( 'content' ) ; } 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' ) ; } if ( $ tag -> hasAttribute ( 'name' ) && $ tag -> getAttribute ( 'name' ) === 'description' ) { $ nonOgDescription = $ tag -> getAttribute ( 'content' ) ; } } 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 ( ! isset ( $ page -> _values [ 'image' ] ) ) { $ domxpath = new DOMXPath ( $ doc ) ; $ elements = $ domxpath -> query ( " //link[@rel='image_src'] " ) ; if ( $ elements -> length > 0 ) { $ domattr = $ elements -> item ( 0 ) -> attributes -> getNamedItem ( 'href' ) ; if ( $ domattr ) { $ page -> _values [ 'image' ] = $ domattr -> value ; $ page -> _values [ 'image_src' ] = $ domattr -> value ; } } } if ( ! isset ( $ page -> _values [ 'image' ] ) ) { $ domxpath = new DOMXPath ( $ doc ) ; $ elements = $ domxpath -> query ( " //link[@rel='apple-touch-icon'][last()] " ) ; if ( $ elements -> length > 0 ) { $ domattr = $ elements -> item ( 0 ) -> attributes -> getNamedItem ( 'href' ) ; if ( $ domattr ) { $ page -> _values [ 'image' ] = $ domattr -> value ; $ page -> _values [ 'image_src' ] = $ domattr -> value ; } } } if ( empty ( $ page -> _values ) ) { return false ; } $ page -> _values [ 'image' ] = ltrim ( $ page -> _values [ 'image' ] , '/' ) ; return $ page ; }
Parses HTML and extracts Open Graph data, this assumes
 the document is at least well formed.
Parses HTML and extracts Open Graph data this assumes the document is at least well formed .
//Added this if loop to retrieve description values from sites like the New York Times who have malformed it.  //Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php //Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php //Fallback to use image_src if ogp::image isn't set. // final fallback to use first image in the page (not working really well) /*
    if (!isset($page->_values['image'])) {
        $domxpath = new DOMXPath($doc);
        $elements = $domxpath->query("//img");
        if ($elements->length > 0) {
            $domattr = $elements->item(0)->attributes->getNamedItem('src');
            if ($domattr) {
                $page->_values['image'] = $domattr->value;
                $page->_values['image_src'] = $domattr->value;
            }
        }
    }
    */