Sunday, May 02, 2010

PHP: Calin Uioreanu's amazon script

Amazon Web Services is a neverending story from transition to transition, that is:

it started of as Amazon Web Services, then became Amazon E-Commerce Service and is currently named Amazon Product Advertising.

in the process of keeping the various scripts i've either written myself or downloaded from different sites over the years running, i took up the amazon script from php9.com by Calin Uioreanu the other day. the website no longer seems to exist and through some new channels now available i was able to contact and inform him about the improvements i had been able to make to the script, which unfortunately i did not document. unfortunately because when Calin Uioreanu came back to me he replied that he had only little time now to play with it. he suggested that if i had a blog perhaps i could show the steps it took to get the script running again.

well, in the end i added a whole lot of other stuff to the script, that's why it's so hard for me to tell which changes i made just to get the basic script running. i'll try though to show just these steps.

persons long familiar and in touch with Amazon Web Services will know that the new version requires a SecretAccessKey in addition to the AWSAccessKeyID that was already required in the past. the user can obtain them by registering with aws.amazon.com. somewhere along the line there's something about fees being charged; however for our purpose to get this script going again it's still free of charge.

once the user has registered with aws.amazon.com and obtained the two keys mentioned above we place them into a separate file named #_aws_keys.inc.php5 (for example) like this:

<?php
$ACCESS_KEY_ID = "xxxxxxxxxxxxxxxxxxxx";
$SECRET_ACCESS_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
?>


and save this file into the common/ folder (for example).

now the trouble starts: i couldn't remember when to include the #_aws_keys.inc.php5 file. (having a good editor that can search through the files is always a good idea.) the #_aws_keys.inc.php5 is included in the product4.php5 file which we can find in the classes/ folder.

best is we now open the product4.php5 file and look for:

function getLiveSearch

then search for the following line:

$searchIndex = $missPlacedNodes[$_GET['browse']];

it should be Line 36. after the closing bracket Line 37 we now insert a whole bunch of code that already helps us to send a signed request to amazon.

here now is a big chunk of code that includes a few extras since i changed my script version to accomodate requests to all amazon locales:

if ($locale == 'US') { $site = 'com'; $partnerId = 'AFFILIATE ID FOR LOCALE US'; }
elseif ($locale == 'UK') { $site = 'co.uk'; $partnerId = 'AFFILIATE ID FOR LOCALE UK'; }
elseif ($locale == 'DE') { $site = 'de'; $partnerId = 'AFFILIATE ID FOR LOCALE DE'; }
elseif ($locale == 'JP') { $site = 'jp'; $partnerId = 'AFFILIATE ID FOR LOCALE JP'; }
elseif ($locale == 'FR') { $site = 'fr'; $partnerId = 'AFFILIATE ID FOR LOCALE FR'; }
elseif ($locale == 'CA') { $site = 'ca'; $partnerId = 'AFFILIATE ID FOR LOCALE CA'; }

include_once('common/#_aws_keys.inc.php5');

$timestamp = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z");
$timestamp = str_replace(':','%3A',$timestamp);

// This is needed for '+' added for space character in request
$requestparams='AWSAccessKeyId='.$ACCESS_KEY_ID.'&AssociateTag='.$partnerId.'&Operation=ItemSearch&SearchIndex='.$searchIndex.'&Keywords='.$searchString.'&ItemPage=1&ResponseGroup='.$responseGroup.'&Service=AWSECommerceService&Timestamp='.$timestamp.'&Version=2009-10-01';
$requestparams = str_replace('+','%2B',$requestparams);

$sort_requestparams = explode("&", $requestparams);
sort($sort_requestparams);

$request = "GET\necs.amazonaws.".$site."\n/onca/xml\n".implode("&",$sort_requestparams);
// $request = "GET\necs.amazonaws.com\n/onca/xml\n".$requestparams;

$request = str_replace(':','%3A',$request);
$request = str_replace(';','%3B',$request);
$request = str_replace(',','%2C',$request);

// Signing
$signature = base64_encode(hash_hmac("sha256",$request,$SECRET_ACCESS_KEY,TRUE));

$signature = str_replace('+','%2B',$signature);
$signature = str_replace('=','%3D',$signature);
$signature = str_replace('/','%2F',$signature);

$xmlUrl = 'http://ecs.amazonaws.'.$site.'/onca/xml?'.$requestparams.'&Signature='.$signature;

// echo '<a href="'.$xmlUrl.'" target="new">The Request</a>';


that last line i uncomment if i run into trouble. it then gives me the request link i have assembled that i can use to check the amazon (XML) document for details, either an error message or the document structure, which i may need in order to access the different XML elements.

to make things very easy i have made my version available for download.

and of course the scripts runs as a live demo.

i hope some users may find this useful. if there's any problems, just leave comments and we'll see how we can improve it further, if and when necessary.

enjoy!

tom.paine


No comments: