Last active
August 29, 2015 14:21
-
-
Save bonnie/9b6877d6b1f0a07d9a9d to your computer and use it in GitHub Desktop.
PHP batch_api example
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
<?php | |
/* general constants for the account and end point */ | |
$UUID = "ABCDEF123456"; | |
$SECRET_KEY = "ABCDEFGHIJKLMNO1234567890123456"; | |
$SERVER = 'https://api-stage.500friends.com'; | |
$ENDPOINT = '/batch_api'; | |
/* create hash for post data */ | |
$post_data = array( | |
"ops" => array( | |
array( | |
"method" => "get", | |
"url" => "/api/ping" | |
), | |
array( | |
"method" => "post", | |
"url" => "/api/record.json", | |
"params" => array( | |
"email" => "[email protected]", | |
"type" => "purchase", | |
"created_at" => "2015-01-30", | |
"value" => "12.34", | |
"detail" => "50 mL face lotion", | |
"event_id" => "ABC123" | |
) | |
), | |
array( | |
"method" => "post", | |
"url" => "/api/record.json", | |
"params" => array( | |
"email" => "[email protected]", | |
"type" => "purchase", | |
"created_at" => "2015-04-28", | |
"value" => "49.99", | |
"detail" => "300 mL eye serum; powder pink lip gloss", | |
"event_id" => "DEF456" | |
) | |
) | |
) | |
); | |
/* calculate signature */ | |
$url_data = array("uuid" => $UUID); | |
$query_string = http_build_query($url_data); | |
$path = $ENDPOINT."?".$query_string; | |
$raw_post = json_encode($post_data, JSON_UNESCAPED_SLASHES); | |
$string_to_hash = $SECRET_KEY . $path . $raw_post; | |
$path = $path . "&sig=" . md5($string_to_hash); | |
$url = $SERVER . $path; | |
/* construct options for the POST request */ | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/json\r\nAccept: application/json\r\n", | |
'method' => 'POST', | |
'content' => $raw_post | |
), | |
); | |
/* debugging */ | |
print "<h1>URL</h1>" . $url; | |
print "<br><br><br><h1>Raw Post</h1>" . $raw_post; | |
/* submit the request */ | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
/* debugging */ | |
print "<br><br><br><h1>Results</h1>"; | |
var_dump($result); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment