Created
April 11, 2013 10:01
-
-
Save dexterbt1/5362182 to your computer and use it in GitHub Desktop.
Basic CLI wyrls REST API client implemented in PHP.
This file contains hidden or 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
#!/usr/bin/env php | |
<?php | |
# dependencies: | |
# php curl extension | |
$usage = "USAGE: {$argv[0]} --client_id=... --privkey=... --url=... --method=... --ctype=... --body=...\n"; | |
$options = getopt('', array('client_id:', 'privkey:', 'url:', 'method:', 'ctype:', 'body:')); | |
$timeout = 60; | |
$client_id = $options['client_id'] or die($usage); | |
$privkey = $options['privkey'] or die($usage); | |
$http_url = $options['url'] or die($usage); | |
$method = $options['method'] or die($usage); | |
$ctype = $options['ctype'] or die($usage); | |
$body = $options['body'] or die($usage); | |
$method = in_array(strtoupper($options['method']), array('GET', 'POST')) ? strtoupper($options['method']) : die("not supported http method `{$options['method']}`"); | |
# ---------- | |
$http_headers = array(); | |
$req_date = gmdate("D, d M Y H:i:s", time())." GMT"; # RFC 1123 | |
$http_headers['Date'] = $req_date; | |
$body_length = strlen($body); | |
$body_b64_md5 = ''; | |
$http_headers['Content-Type'] = $ctype; | |
$body_b64_md5 = base64_encode(pack('H*', md5($body))); | |
$http_headers['Content-Length'] = $body_length; | |
$uri = parse_url($http_url); | |
$string_to_sign = $method . "\n" | |
. $uri['path'] . "\n" | |
. $req_date . "\n" | |
. $ctype . "\n" | |
. $body_length . "\n" | |
. $body_b64_md5 . "\n" | |
; | |
echo "\n"; | |
echo "---------------\n"; | |
echo "string to sign:\n"; | |
echo "---------------\n"; | |
echo "[$string_to_sign]\n"; | |
echo "\n\n"; | |
// RSA private key is in the file | |
// PHP docs in using openssl_* for more on how to manage keys | |
$privkey = openssl_pkey_get_private(file_get_contents($options['privkey'])) | |
or die("invalid ssh private key {$options['privkey']}"); | |
openssl_sign($string_to_sign, $signature, $privkey); | |
openssl_free_key($privkey); | |
$signature_b64 = base64_encode($signature); | |
$http_headers['Authorization'] = "MCWS $client_id:$signature_b64"; | |
$curl_http_headers = array(); | |
foreach ($http_headers as $key => $value) { | |
array_push($curl_http_headers, $key.": ".$value); | |
} | |
$defaults = array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_CUSTOMREQUEST => $method, // set request method | |
CURLOPT_HTTPHEADER => $curl_http_headers, // set request headers | |
CURLOPT_URL => $http_url, | |
CURLOPT_FRESH_CONNECT => 1, | |
CURLOPT_FORBID_REUSE => 1, | |
CURLOPT_TIMEOUT => $timeout, | |
CURLOPT_HEADER => 1, // capture response headers | |
CURLOPT_POSTFIELDS => $body, | |
CURLOPT_VERBOSE => TRUE, // debug | |
); | |
$ch = curl_init(); | |
curl_setopt_array($ch, $defaults); | |
$result = curl_exec($ch); | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
echo "\n"; | |
echo "---------\n"; | |
echo "response:\n"; | |
echo "---------\n"; | |
print_r($result); | |
echo "\n\n"; | |
echo "\n"; | |
echo "-------------------\n"; | |
echo "response curl info:\n"; | |
echo "-------------------\n"; | |
print_r($info); | |
echo "\n\n"; | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment