Created
December 11, 2013 06:50
-
-
Save mtasuandi/7906039 to your computer and use it in GitHub Desktop.
Morguefile API Class
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 morguefile { | |
private $api_id; | |
private $api_secret; | |
function __construct($api_id, $api_secret) { | |
if(!function_exists('curl_init')){ | |
throw new Exception('Curl is required for morguefile API'); | |
} | |
$this->api_id = $api_id; | |
$this->api_secret = $api_secret; | |
} | |
public function call($parms, $method='json'){ | |
$o = $this->cleanParamString($parms); | |
if(!empty($o)){ | |
if($method!='json' && $method!='xml'){ | |
$method = 'json'; | |
} | |
/* create the signature */ | |
$sig = hash_hmac("sha256", $o['str'], $this->api_secret); | |
/* create the api call */ | |
$c = curl_init ('https://morguefile.com/api/' . $o['uri'] . '.'.$method ); | |
curl_setopt ($c, CURLOPT_POST, true); | |
curl_setopt ($c, CURLOPT_POSTFIELDS, 'key='.$this->api_id.'&sig='.$sig); | |
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt ($c, CURLOPT_SSL_VERIFYPEER, false); | |
$page = curl_exec ($c); | |
if(!empty($page)){ | |
if($method=='json'){ | |
$data = json_decode($page); | |
} else { | |
$data = ($page); | |
} | |
return $data; | |
} else { | |
throw new Exception(curl_error($c)); | |
} | |
} else { | |
throw new Exception('Malformed string'); | |
} | |
curl_close ($c); | |
} | |
private function cleanParamString($parms){ | |
/* clean up the url string to avoid errors */ | |
$parms = trim(strtolower($parms)); | |
$p = explode('/', $parms); | |
$p = array_filter($p, 'strlen'); | |
if(!empty($p)) { | |
$o['str'] = implode('', $p); | |
$o['uri'] = implode('/', $p) . '/'; | |
return $o; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment