├── README.md ├── OpenGraphTest.php └── OpenGraph.php /README.md: -------------------------------------------------------------------------------- 1 | # Open Graph Protocol helper for PHP 2 | 3 | A small library for making accessing of Open Graph Protocol data easier 4 | 5 | ## Note 6 | Keys with a dash (-) in the name are converted to _ for easy access as a property 7 | in PHP 8 | 9 | ## Required Extensions 10 | * DOM for parsing 11 | 12 | ## Usage 13 | require_once('OpenGraph.php'); 14 | 15 | $graph = OpenGraph::fetch('http://www.rottentomatoes.com/m/10011268-oceans/'); 16 | var_dump($graph->keys()); 17 | var_dump($graph->schema); 18 | 19 | foreach ($graph as $key => $value) { 20 | echo "$key => $value"; 21 | } -------------------------------------------------------------------------------- /OpenGraphTest.php: -------------------------------------------------------------------------------- 1 | assertType('OpenGraph', $o); 13 | 14 | $this->assertAttributeEquals( 15 | array( 16 | 'title' => 'Oceans', 17 | 'type' => 'movie', 18 | 'image' => 'http://images.rottentomatoes.com/images/movie/custom/68/10011268.jpg', 19 | 'url' => 'http://www.rottentomatoes.com/m/10011268-oceans/', 20 | 'site_name' => 'Rotten Tomatoes', 21 | ), 22 | '_values', 23 | $o 24 | ); 25 | } 26 | 27 | public function testFetchReturnsFalseForWebsiteWithNoOpenGraphMetadata() 28 | { 29 | $this->assertEquals(FALSE, OpenGraph::fetch('http://www.example.org/')); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /OpenGraph.php: -------------------------------------------------------------------------------- 1 | array('activity', 'sport'), 27 | 'business' => array('bar', 'company', 'cafe', 'hotel', 'restaurant'), 28 | 'group' => array('cause', 'sports_league', 'sports_team'), 29 | 'organization' => array('band', 'government', 'non_profit', 'school', 'university'), 30 | 'person' => array('actor', 'athlete', 'author', 'director', 'musician', 'politician', 'public_figure'), 31 | 'place' => array('city', 'country', 'landmark', 'state_province'), 32 | 'product' => array('album', 'book', 'drink', 'food', 'game', 'movie', 'product', 'song', 'tv_show'), 33 | 'website' => array('blog', 'website'), 34 | ); 35 | 36 | /** 37 | * Holds all the Open Graph values we've parsed from a page 38 | * 39 | */ 40 | private $_values = array(); 41 | 42 | /** 43 | * Fetches a URI and parses it for Open Graph data, returns 44 | * false on error. 45 | * 46 | * @param $URI URI to page to parse for Open Graph data 47 | * @return OpenGraph 48 | */ 49 | static public function fetch($URI) { 50 | return self::_parse(file_get_contents($URI)); 51 | } 52 | 53 | /** 54 | * Parses HTML and extracts Open Graph data, this assumes 55 | * the document is at least well formed. 56 | * 57 | * @param $HTML HTML to parse 58 | * @return OpenGraph 59 | */ 60 | static private function _parse($HTML) { 61 | $old_libxml_error = libxml_use_internal_errors(true); 62 | 63 | $doc = new DOMDocument(); 64 | $doc->loadHTML($HTML); 65 | 66 | libxml_use_internal_errors($old_libxml_error); 67 | 68 | $tags = $doc->getElementsByTagName('meta'); 69 | if (!$tags || $tags->length === 0) { 70 | return false; 71 | } 72 | 73 | $page = new self(); 74 | 75 | foreach ($tags AS $tag) { 76 | if ($tag->hasAttribute('property') && 77 | strpos($tag->getAttribute('property'), 'og:') === 0) { 78 | $key = strtr(substr($tag->getAttribute('property'), 3), '-', '_'); 79 | $page->_values[$key] = $tag->getAttribute('content'); 80 | } 81 | } 82 | 83 | if (empty($page->_values)) { return false; } 84 | 85 | return $page; 86 | } 87 | 88 | /** 89 | * Helper method to access attributes directly 90 | * Example: 91 | * $graph->title 92 | * 93 | * @param $key Key to fetch from the lookup 94 | */ 95 | public function __get($key) { 96 | if (array_key_exists($key, $this->_values)) { 97 | return $this->_values[$key]; 98 | } 99 | 100 | if ($key === 'schema') { 101 | foreach (self::$TYPES AS $schema => $types) { 102 | if (array_search($this->_values['type'], $types)) { 103 | return $schema; 104 | } 105 | } 106 | } 107 | } 108 | 109 | /** 110 | * Return all the keys found on the page 111 | * 112 | * @return array 113 | */ 114 | public function keys() { 115 | return array_keys($this->_values); 116 | } 117 | 118 | /** 119 | * Helper method to check an attribute exists 120 | * 121 | * @param $key 122 | */ 123 | public function __isset($key) { 124 | return array_key_exists($key, $this->_values); 125 | } 126 | 127 | /** 128 | * Will return true if the page has location data embedded 129 | * 130 | * @return boolean Check if the page has location data 131 | */ 132 | public function hasLocation() { 133 | if (array_key_exists('latitude', $this->_values) && array_key_exists('longitude', $this->_values)) { 134 | return true; 135 | } 136 | 137 | $address_keys = array('street_address', 'locality', 'region', 'postal_code', 'country_name'); 138 | $valid_address = true; 139 | foreach ($address_keys AS $key) { 140 | $valid_address = ($valid_address && array_key_exists($key, $this->_values)); 141 | } 142 | return $valid_address; 143 | } 144 | 145 | /** 146 | * Iterator code 147 | */ 148 | private $_position = 0; 149 | public function rewind() { reset($this->_values); $this->_position = 0; } 150 | public function current() { return current($this->_values); } 151 | public function key() { return key($this->_values); } 152 | public function next() { next($this->_values); ++$this->_position; } 153 | public function valid() { return $this->_position < sizeof($this->_values); } 154 | } 155 | --------------------------------------------------------------------------------