Skip to content

Instantly share code, notes, and snippets.

@jdorfman
Created May 23, 2012 16:10
Show Gist options
  • Save jdorfman/2776146 to your computer and use it in GitHub Desktop.
Save jdorfman/2776146 to your computer and use it in GitHub Desktop.
<?php
/*
* NetDNA API Command Line Utility - PHP
* Version 1.0a
*/
// Get it here: https://raw.github.com/gist/2791330/64b7007ab9d4d4cbb77efd107bb45e16fc6c8cdf/OAuth.php
require_once("OAuth.php");
//your app key and secret
$key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$alias = "companyalias";
// create an OAuth consumer with your key and secret
$consumer = new OAuthConsumer($key, $secret, NULL);
// method type: GET, POST, etc
$method_type = "GET";
//url to send request to (everything after alias/ in endpoint)
$selected_call = "zones.json";
// the endpoint for your request
$endpoint = "https://rws.netdna.com/$alias/$selected_call"; //this endpoint will pull the account information for the provided alias
//parse endpoint before creating OAuth request
$parsed = parse_url($endpoint);
if(array_key_exists("parsed", $parsed))
{
parse_str($parsed['query'], $params);
}
//generate a request from your consumer
$req_req = OAuthRequest::from_consumer_and_token($consumer, NULL, $method_type, $endpoint, $params);
//sign your OAuth request using hmac_sha1
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$req_req->sign_request($sig_method, $consumer, NULL);
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $req_req);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE);
// set curl custom request type if not standard
if ($method_type != "GET" && $method_type != "POST") {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_type);
}
if ($method_type == "POST" || $method_type == "PUT" || $method_type == "DELETE") {
$query_str = OAuthUtil::build_http_query($params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Length: ' . strlen($query_str)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_str);
}
//tell curl to grab headers
//curl_setopt($ch, CURLOPT_HEADER, true);
// $output contains the output string
$json_output = curl_exec($ch);
// $headers contains the output headers
// $headers = curl_getinfo($ch);
// close curl resource to free up system resources
curl_close($ch);
//convert json response into multi-demensional array
$ouput = json_decode($json_output, true);
// dump the result
if(is_null($output) || empty($output)) {
var_dump($json_output);
} else {
var_dump($output);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment