Created
September 8, 2010 02:29
-
-
Save jdp/569515 to your computer and use it in GitHub Desktop.
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 Scriblr { | |
private $api_key; | |
private $api_secret; | |
private $publisher_id; | |
private $method_family = NULL; | |
public $http_status; | |
public $last_api_call; | |
function __construct($api_key, $api_secret, $publisher_id = NULL, $method_family = NULL) { | |
$this->api_key = $api_key; | |
$this->api_secret = $api_secret; | |
$this->publisher_id = $publisher_id; | |
$this->method_family = $method_family; | |
} | |
function __get($family) { | |
$class_name = get_class($this); | |
$ref = new $class_name($this->api_key, $this->api_secret, $this->publisher_id, $family); | |
return $ref; | |
} | |
function __call($name, $args) { | |
$parameters = (count($args) > 0) ? $args[0] : array(); | |
$parameters = array_merge($parameters, array( | |
'method' => isset($this->method_family) ? sprintf('%s.%s', $this->method_family, $name) : $name, | |
'api_key' => $this->api_key | |
)); | |
ksort($parameters); | |
$api_sig = md5($this->api_secret.implode(array_map(function($key, $value) { | |
return $key.$value; | |
}, array_keys($parameters), array_values($parameters)))); | |
$parameters['api_sig'] = $api_sig; | |
$query = http_build_query($parameters); | |
$api_call = sprintf('http://api.scribd.com/api?%s', $query); | |
return $this->request($api_call); | |
} | |
protected function request($api_url) { | |
$curl_handle = curl_init(); | |
curl_setopt($curl_handle, CURLOPT_URL, $api_url); | |
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