Created
March 26, 2012 23:12
-
-
Save netdna/2210490 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 | |
// Download - https://gist.github.com/raw/2210583/64b7007ab9d4d4cbb77efd107bb45e16fc6c8cdf/OAuth.php | |
require_once("OAuth.php"); | |
// Download - https://gist.github.com/raw/2210597/cdf6262b41e1de4562a020ad4cb371cbb9425f03/OAuthServer.php | |
require_once("OAuthServer.php"); | |
$test_server = new TestOAuthServer(new MockOAuthDataStore()); | |
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1(); | |
$plaintext_method = new OAuthSignatureMethod_PLAINTEXT(); | |
$rsa_method = new TestOAuthSignatureMethod_RSA_SHA1(); | |
$test_server->add_signature_method($hmac_method); | |
$test_server->add_signature_method($plaintext_method); | |
$test_server->add_signature_method($rsa_method); | |
$sig_methods = $test_server->get_signature_methods(); | |
// - /api/apps/create | |
$key = "sldkjaslkdjaslkdj"; | |
$secret = "alksdjalskjdlaksjdlasijd"; | |
$endpoint = $argv[1]; | |
$plain = $argv[2]; | |
$dump_request = false; | |
$sig_method = $hmac_method; | |
$test_consumer = new OAuthConsumer($key, $secret, NULL); | |
function run($endpoint, $test_consumer, $sig_method, $plain, $dump_request = false) | |
{ | |
$parsed = parse_url($endpoint); | |
$params = array(); | |
if(array_key_exists("parsed", $parsed)) | |
{ | |
parse_str($parsed['query'], $params); | |
} | |
$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $endpoint, $params); | |
$req_req->sign_request($sig_method, $test_consumer, NULL); | |
if ($dump_request) { | |
Header('Content-type: text/plain'); | |
print "request url: " . $req_req->to_url(). "\n"; | |
print_r($req_req); | |
exit; | |
} | |
// 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); | |
// $output contains the output string | |
$output = curl_exec($ch); | |
// close curl resource to free up system resources | |
curl_close($ch); | |
parse($output,$plain); | |
} | |
function parse($return, $plain = true) | |
{ | |
if($plain) | |
{ | |
echo $return; | |
} | |
else | |
{ | |
$return = json_decode( $return, true); | |
if(array_key_exists("code",$return)) | |
{ | |
if($return['code'] == 200) | |
{ | |
$elements = $return['data']; | |
print_r($elements); | |
} | |
else | |
{ | |
echo "Error: " . $return['code'] . ":"; | |
$elements = $return['error']; | |
foreach($elements as $key => $value) | |
{ | |
echo "$key = $value"; | |
} | |
} | |
} | |
else | |
{ | |
echo "No return code given"; | |
} | |
} | |
die(); | |
} | |
run($endpoint,$test_consumer,$hmac_method,$plain); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment