On August 15, 2009, all Product Advertising API developers were required to authenticate ALL calls using a hmac/sha256 signing token. For some reason, this was a major pain in the ass for me, but I finally got things to work.
If you’re having trouble, and you’re using PHP5, this may help you.
The first item you’ll need is the ‘secret’. You need to log into the Amazon Web Services area (https://developer.amazonwebservices.com). After logging in (assuming you already have a account if you’re using their API), you need to hover over ‘Your Account’, and click ‘Access Identifiers’. Here you will see ‘Access Key ID’ and ‘Secret Access Key’. We’ll be referring Access Key ID as the public key and Secret Access Key as the private key in a function provided below. Copy these two values and add them to your code somewhere.
You will need to ensure that PHP is built with hash support. You can verify this with by view info (<?phpinfo()?>). If hash support is there we can use the built in hash_hmac() function. The concept is pretty straight forward (eventhough, I’ve been fighting it all night prior to this post). You create a string, and ‘hash’ it with our private key, then we pass the string to Amazon’s API, and they hash the same string with our private key. The value should be the same, and if so, we’re good. If something is off any where in that sequence you’ll be getting the dreaded ‘SignatureDoesNotMatch‘ message in your XML return.
I came across Ulrich Mierendorff’s function for sending a signed AWS request on the developer forum, and it was the only PHP code snippet that worked for me, so I need to pass it on. Below is the function:
Copyright (c) 2009 Ulrich Mierendorff
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
$region – the Amazon(r) region (ca,com,co.uk,de,fr,jp)
$params – an array of parameters, eg. array("Operation"=>"ItemLookup",
$public_key – your "Access Key ID"
$private_key – your "Secret Access Key"
// some paramters
$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";
// additional parameters
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
// GMT timestamp
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
// API version
$params["Version"] = "2009-03-31";
// sort the parameters
// create the canonicalized query
$canonicalized_query = array();
foreach ($params as $param=>$value)
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query = $param."=".$value;
$canonicalized_query = implode("&", $canonicalized_query);
// create the string to sign
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
// calculate HMAC with SHA256 and base64-encoding
$signature = base64_encode(hash_hmac(‘sha256’, $string_to_sign, $private_key, True));
// create request
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
// do request
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $request);
$response = curl_exec($ch);
if ($response === False)
// parse XML
$pxml = simplexml_load_string($response);
if ($pxml === False)
return False; // no xml
To call the function you create a array of arguments and pass it to the function along with the keys values.
$public_key = "asfdasdfasdfasfdasfda";
$private_key = "SECRETKEYasfdasdfasdfasfdasfda";
$param = array("Operation"=>"ItemSearch",
$xml = aws_signed_request("com",$param, $public_key, $private_key);
This worked perfectly for me, and got my Amazon sites back on line. Hope this helps!!