Skip to content

Commit

Permalink
Open Graph API helper in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott MacVicar committed Apr 26, 2010
0 parents commit a95c17e
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
165 changes: 165 additions & 0 deletions OpenGraph.php
@@ -0,0 +1,165 @@
<?php
/*
Copyright 2010 Scott MacVicar
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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.
*/

class OpenGraph implements Iterator
{
/**
* There are base schema's based on type, this is just
* a map so that the schema can be obtained
*
*/
public static $TYPES = array(
'activity' => array('activity', 'sport'),
'business' => array('bar', 'company', 'cafe', 'hotel', 'restaurant'),
'group' => array('cause', 'sports_league', 'sports_team'),
'organization' => array('band', 'government', 'non_profit', 'school', 'university'),
'person' => array('actor', 'athlete', 'author', 'director', 'musician', 'politician', 'public_figure'),
'place' => array('city', 'country', 'landmark', 'state_province'),
'product' => array('album', 'book', 'drink', 'food', 'game', 'movie', 'product', 'song', 'tv_show'),
'website' => array('blog', 'website'),
);

/**
* Holds all the Open Graph values we've parsed from a page
*
*/
private $_values = array();

/**
* Fetches a URI and parses it for Open Graph data, returns
* false on error.
*
* @param $URI URI to page to parse for Open Graph data
* @return OpenGraph
*/
static public function fetch($URI) {
return self::_parse(file_get_contents($URI));
}

/**
* Parses HTML and extracts Open Graph data, this assumes
* the document is at least well formed.
*
* @param $HTML HTML to parse
* @return OpenGraph
*/
static private function _parse($HTML) {
$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();

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 (empty($page->_values)) { return false; }

return $page;
}

/**
* Helper method to access attributes directly
* Example:
* $graph->title
*
* @param $key Key to fetch from the lookup
*/
public function __get($key) {
if (array_key_exists($key, $this->_values)) {
return $this->_values[$key];
}

if ($key === 'schema') {
foreach (self::$TYPES AS $schema => $types) {
if (array_search($this->_values['type'], $types)) {
return $schema;
}
}
}
}

/**
* Return all the keys found on the page
*
* @return array
*/
public function keys() {
return array_keys($this->_values);
}

/**
* Helper method to check an attribute exists
*
* @param $key
*/
public function __isset($key) {
return array_key_exists($key, $this->_values);
}

/**
* Will return true if the page has location data embedded
*
* @return boolean Check if the page has location data
*/
public function hasLocation() {
if (array_key_exists('latitude', $this->_values) && array_key_exists('longitude', $this->_values)) {
return true;
}

$address_keys = array('street_address', 'locality', 'region', 'postal_code', 'country_name');
$valid_address = true;
foreach ($address_keys AS $key) {
$valid_address = ($valid_address && array_key_exists($key, $this->_values));
}
return $valid_address;
}

/**
* Iterator code
*/
private $_position = 0;
public function rewind() { reset($this->_values); $this->_position = 0; }
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); }
}

/* tests
$test = OpenGraph::fetch('http://www.rottentomatoes.com/m/10011268-oceans/');
var_dump($test->keys());
foreach ($test AS $key => $value) {
var_dump($key, $value);
}
$test = OpenGraph::fetch('http://www.example.org');
var_dump($test);
*/
21 changes: 21 additions & 0 deletions README.md
@@ -0,0 +1,21 @@
# Open Graph Protocol helper for PHP

A small library for making accessing of Open Graph Protocol data easier

## Note
Keys with a dash (-) in the name are converted to _ for easy access as a property
in PHP

## Required Extensions
* DOM for parsing

## Usage
require_once('OpenGraph.php');

$graph = OpenGraph::fetch('http://www.rottentomatoes.com/m/10011268-oceans/');
var_dump($graph->keys());
var_dump($graph->schema);

foreach ($graph as $key => $value) {
echo "$key => $value";
}

0 comments on commit a95c17e

Please sign in to comment.