<?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" => "example@example.com",
"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" => "example@example.com",
"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);
?>
PHP batch_api example
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.