Created
May 19, 2009 08:26
-
-
Save jdp/113977 to your computer and use it in GitHub Desktop.
Glue API implementation
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 | |
// require glue lib | |
require 'glue.php'; | |
// create a glue object, and look up the neverending story in json format | |
// $glue->$method->$chain maps to api calls, like method/chain | |
// you can use any value, but glue will throw errors | |
$glue = new Glue('username', 'password'); | |
$response = $glue->object->get(array( | |
'objectId' => 'http://en.wikipedia.org/wiki/The_Neverending_Story', | |
)); | |
// pretty-print result xml | |
echo '<pre>'; | |
print_r(simplexml_load_string($response)); | |
echo '</pre>'; | |
?> |
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 Glue { | |
private $__username; | |
private $__password; | |
private $__method_family; | |
private $credentials; | |
private $http_status; | |
private $last_api_call; | |
function __construct($username, $password, $method_family = null) { | |
$this->__username = $username; | |
$this->__password = $password; | |
if (isset($method_family)) { | |
$this->__method_family = $method_family; | |
} | |
else { | |
unset($this->__method_family); | |
} | |
$this->credentials = sprintf('%s:%s', $username, $password); | |
} | |
function __get($var) { | |
$class_name = get_class($this); | |
$ref = new $class_name($this->__username, $this->__password, $var); | |
return $ref; | |
} | |
function __call($name, $args) { | |
$fields = http_build_query((count($args) > 0) ? $args[0] : array()); | |
$method = (isset($this->__method_family)) ? sprintf('%s/%s', $this->__method_family, $name) : $name; | |
$api_call = sprintf('http://api.getglue.com/v1/%s?%s', $method, $fields); | |
return $this->request($api_call); | |
} | |
function request($api_url) { | |
$curl_handle = curl_init(); | |
curl_setopt($curl_handle, CURLOPT_URL, $api_url); | |
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials); | |
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($curl_handle); | |
$this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); | |
curl_close($curl_handle); | |
$this->last_api_call = $api_url; | |
return $response; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment