Created
March 13, 2012 17:14
-
-
Save lastguest/2029977 to your computer and use it in GitHub Desktop.
Open Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ... | |
*/ | |
class OpenGraphNode extends OpenGraph | |
{ | |
public $id; | |
public $object; | |
private $connections; | |
function __construct($id) { | |
parent::__construct(); | |
$this -> id = $id; | |
$tmp = $this -> api($this -> id . '?metadata=1'); | |
$this -> connections = $tmp -> metadata -> connections; | |
unset($tmp -> metadata); | |
$this -> object = $tmp; | |
} | |
/* | |
* toString magic method (JSON output) | |
*/ | |
public function __toString(){ | |
return json_encode($this->object); | |
} | |
function __get($name) { | |
if (isset($this -> connections -> $name)) { | |
return new OpenGraphNodeList($this -> connections -> $name); | |
} | |
elseif (isset($this -> object -> $name)) { | |
return $this -> object -> $name; | |
} | |
else { | |
throw new Exception(); | |
} | |
} | |
} | |
/** | |
* ... | |
*/ | |
class OpenGraphNodeList extends OpenGraph implements SeekableIterator | |
{ | |
private $pointer = 0; | |
public $data; | |
public $paging; | |
function __construct($path) { | |
parent::__construct(); | |
$tmp = $this -> api($path); | |
if (isset($tmp -> error)) { | |
$ExceptionType = $tmp -> error -> type; | |
throw new $ExceptionType($tmp -> error -> message, $tmp -> error -> code); | |
} | |
$this -> data = $tmp -> data; | |
if (isset($tmp -> paging)) { | |
$this -> paging = $tmp -> paging; | |
$this -> paging -> current = $path; | |
} | |
} | |
/* | |
* toString magic method (JSON output) | |
*/ | |
public function __toString(){ | |
return json_encode($this->data); | |
} | |
public function seek($position) { | |
while (!isset($this -> data[$position])) { | |
$this -> next(); | |
} | |
$this -> pointer = $position; | |
return $this -> current(); | |
} | |
function rewind() { | |
$this -> pointer = 0; | |
} | |
function current() { | |
return new OpenGraphNode($this -> data[$this -> pointer] -> id); | |
} | |
function key() { | |
return $this -> pointer; | |
} | |
function next() { | |
++$this -> pointer; | |
if (!isset($this -> data[$this -> pointer]) && isset($this -> paging -> next)) { | |
$tmp = new OpenGraphNodeList($this -> paging -> next); | |
if (isset($tmp -> data[0])) { | |
foreach ($tmp -> data as $k => $v) { | |
array_push($this -> data, $v); | |
} | |
} | |
} | |
elseif (!isset($this -> data[$this -> pointer])) { | |
throw new OutOfBoundsException('Invalid next position (' . $this -> pointer . ')'); | |
} | |
} | |
function valid() { | |
return isset($this -> data[$this -> pointer]); | |
} | |
} | |
/** | |
* ... | |
*/ | |
class OpenGraph | |
{ | |
private $curl; | |
public $access_token = 'AAACfS1ZAM1AwBAHYg9XMWWsz0nvSepgvgB6Np8U5sC2TqY9Ey4KyeR2nm2ZAWPZAbCbsHIfgUbb0IqeBr4qSavdPZAJ9QqHOGe2I8qLPKrxavKzmcZCH6'; | |
function __construct() { | |
$this -> curl = curl_init(); | |
curl_setopt($this -> curl, CURLOPT_HEADER, 0); | |
curl_setopt($this -> curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($this -> curl, CURLOPT_SSL_VERIFYPEER, 0); | |
} | |
function __destruct() { | |
curl_close($this -> curl); | |
} | |
protected function api($path) { | |
if (!filter_var($path, FILTER_VALIDATE_URL)) { | |
$path = 'http://graph.facebook.com/' . $path; | |
} | |
$parsed_url = parse_url($path); | |
$parsed_url['scheme'] = 'https'; | |
parse_str((isset($parsed_url['query']) ? $parsed_url['query'] : ''), $parsed_str); | |
if (!isset($parsed_str['access_token'])) { | |
$parsed_str['access_token'] = $this -> access_token; | |
} | |
$parsed_url['query'] = http_build_query($parsed_str); | |
$path = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . '?' . $parsed_url['query']; | |
curl_setopt($this -> curl, CURLOPT_URL, $path); | |
return json_decode(curl_exec($this -> curl)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment