Skip to content

Instantly share code, notes, and snippets.

@kressaty
Created September 14, 2010 00:39
Show Gist options
  • Save kressaty/578336 to your computer and use it in GitHub Desktop.
Save kressaty/578336 to your computer and use it in GitHub Desktop.
<?php
$account_id = '#'; //your account id
$path = "accounts/$account_id/limits.xml"; //the method or function you're calling via the api
$subdomain = "subdomain"; //your subdomain
$username = "api_key"; // your api key
$password = "password"; //your password
$method = "GET"; //method for the path you've chosen
//do the request
$output = request($path, $subdomain, $username, $password, $method);
//print the results to the screen
echo "<pre>";
print_r($output);
echo "</pre>";
function request($path, $subdomain, $username, $password, $method = "POST", $vars = array()) {
$endpoint = "http://$subdomain.authoritylabs.com/";
$encoded = "";
foreach($vars AS $key=>$value)
$encoded .= "$key=".urlencode($value)."&";
$encoded = substr($encoded, 0, -1);
$tmpfile = "";
$fp = null;
// construct full url
$url = $endpoint.$path;
// if GET and vars, append them
if($method == "GET")
$url .= (FALSE === strpos($path, '?')?"?":"&").$encoded;
// initialize a new curl object
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
switch(strtoupper($method)) {
case "GET":
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
break;
case "POST":
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
break;
case "PUT":
// curl_setopt($curl, CURLOPT_PUT, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
file_put_contents($tmpfile = tempnam("/tmp", "put_"),
$encoded);
curl_setopt($curl, CURLOPT_INFILE, $fp = fopen($tmpfile,
'r'));
curl_setopt($curl, CURLOPT_INFILESIZE,
filesize($tmpfile));
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
return "Unknown method $method";
break;
}
// send credentials
curl_setopt($curl, CURLOPT_USERPWD,
$pwd = $username.":".$password);
// do the request. If FALSE, then an exception occurred
if(FALSE === ($result = curl_exec($curl)))
return "Curl failed with error " . curl_error($curl);
// get result code
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// unlink tmpfiles
if($fp)
fclose($fp);
if(strlen($tmpfile))
unlink($tmpfile);
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment