Skip to content

Instantly share code, notes, and snippets.

@wkoszek
Created September 10, 2018 04:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkoszek/ebddd10c719089692c4b3fe9f7c1f673 to your computer and use it in GitHub Desktop.
Save wkoszek/ebddd10c719089692c4b3fe9f7c1f673 to your computer and use it in GitHub Desktop.
<?php
// Your Access Key ID, as taken from the Your Account page
$access_key_id = "AKIAAAAABBBBCCCCDDDD";
// Your Secret Key corresponding to the above ID, as taken from the Your Account page
$secret_key = "mysecretkeyishereanditworkswhenirunit___";
// The region you are interested in
$endpoint = "webservices.amazon.com";
$uri = "/onca/xml";
$params = array(
"Service" => "AWSECommerceService",
"Operation" => "ItemSearch",
"AWSAccessKeyId" => $access_key_id,
"AssociateTag" => "wkoszek08-20",
"SearchIndex" => "All",
"Keywords" => "outsiders william thorndike",
"ResponseGroup" => "Images,ItemAttributes,Offers"
);
// Set current timestamp if not set
if (!isset($params["Timestamp"])) {
$params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
}
// Sort the parameters by key
ksort($params);
$pairs = array();
foreach ($params as $key => $value) {
array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
}
// Generate the canonical query
$canonical_query_string = join("&", $pairs);
// Generate the string to be signed
$string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string;
// Generate the signature required by the Product Advertising API
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $secret_key, true));
// Generate the signed URL
$request_url = 'https://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode($signature);
//echo "Signed URL: \"".$request_url."\"";
$xml_str = file_get_contents($request_url);
//echo "$xml_str";
$parsed_xml = simplexml_load_string($xml_str);
print "\n\n\n\n\n\n";
print $parsed_xml;
print "\n\n\n\n\n\n";
foreach($parsed_xml->Items->Item as $current){
print_r($current);
print("<b>".$current->ItemAttributes->Title."</b><br />");
print("<i>".$current->ItemAttributes->Author."</i><br />");
print("\n\n");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment