Creating Amazon signature with PHP

Internet’s largest online store Amazon.com introduced application programming interface or API several years ago. Since the dawn of this API it has been extremely successful and popular. Many websites spice up their content with product information from Amazon’s vast range of items and link back to Amazon’s store in order to generate some affiliate revenue. Making calls to Amazon webservices is relatively easy and straightforward. Simple requests can be formed with just few lines of PHP code. This August will slightly change the call as after 15 August 2009 all product advertising API calls must be supplied with mandatory time stamp and signature parameters.

I wrote a short PHP code snippet to calculate the required signature. The product advertising API documentation has thorough instructions on how to add the signature. The following little program does the trick.

<?php $secretAccessKey = 'your secret access key'; $parameters = array(   'Service' => 'AWSECommerceService'   ,'AWSAccessKeyId' => 'your AWS access key ID'   ,'AssociateTag' => 'your associate tag'   ,'Version' => '2009-01-06'   ,'Timestamp' => gmdate('Y-m-dTH:i:sZ')   ,'Operation' => 'ItemSearch'   ,'ResponseGroup' => 'Small,Images'   ,'SearchIndex' => 'Blended'   ,'Keywords' => 'your keywords' ); ksort($parameters); $s = ''; foreach ($parameters as $name => $value) {   $s .= (strlen($s) > 0 ? '&' : '') . $name . '='     . rawurlencode($value); } // calculate signature $raw = "GET\nwebservices.amazon.com\n/onca/xml\n" . $s; $signature = base64_encode(   hash_hmac('sha256', $raw, $secretAccessKey, true) ); $url = 'http://webservices.amazon.com/onca/xml?' . $s   . '&Signature=' . rawurlencode($signature); ?>

To authenticate the request you need to obtain three keys from Amazon. You will need your associate ID and Amazon Web Services access key ID as well as the secret access key. All these can be found in your AWS account. The code above starts with collecting all the parameters needed to make a request to Amazon. You may change the parameters to suit your needs accordingly. This sample code queries a bunch of items with images matching entered keywords. The next step is to sort the parameters as directed in the API documentation. PHP ksort function sorts an array based on its keys while maintaining the associated values. After sorting the values they are joined into a single string while encoding the values. The string is then used to calculate the signature with the secret access key. Finally, the complete URL for the request is combined with the calculated signature and ready for a call.

Final words

Change in Amazon API certainly serves the quality of the service bringing some more security identifying and verifying the sender of the request. It also requires changes in all websites using Amazon product advertising API. Modifications are minor and easy to implement even in the existing code. Visit the API documentation site for more information and examples.

Julkaistu sunnuntaina 21.6.2009 klo 17:49 avainsanoilla Internet ja ohjelmointi.

Edellinen
Kirjoitetaanko juhannus isolla vai pienellä alkukirjaimella
Seuraava
Piraattipalvelu