Inside things

Thursday, July 09, 2009

Inside Signed AWS with PHP and SOAP

Today I decided to update my AWS (Amazon Web Services) code (now called Product Advertising API) to use the new signed requests. Soon all request will need to be signed or else Amazon will just ignore them. I came with what I think to be a pretty elegant solution so I thought I could share it with the world. It uses the SOAP version of the service, and will only work on PHP >= 5.1. Any comments will be appreciated.

function aws_call($action, $body, $locale = 'US')
{
$access_key_id = 'your public access key';
$secret_access_key = 'your secret access key';

$locale_url = '';
if ($locale != 'US') {
$locale_url = "$locale/";
}
$client = new SoapClient('http://ecs.amazonaws.com/AWSECommerceService/2009-06-01/' . $locale_url . 'AWSECommerceService.wsdl');

$timestamp = gmdate("Y-m-d\TH:i:s\Z");
$signature = $action . $timestamp;
$signature = base64_encode(hash_hmac("sha256", $signature, $secret_access_key, True));
$headers = array();
$headers[] = new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/',
'AWSAccessKeyId',
$access_key_id);
$headers[] = new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/',
'Timestamp',
$timestamp);
$headers[] = new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/',
'Signature',
$signature);

$result = $client->__soapCall($action, array($body), NULL, $headers);
}

An example for calling this function

$body = array (
"SubscriptionId" => "your subs-ID",
"Request" => array (
"ResponseGroup" => "EditorialReview,Images",
"ItemId" => $this->asin,
"IdType" => "ASIN",
)
);

$result = aws_call('ItemLookup', $body);

Hope you like it and find it useful.

Labels:

Tuesday, February 14, 2006



Esta es una excelente foto del bosque de Santa Elena en Costa Rica. Me costò demasiado tomarla. Pero por eso quedò tan buena. Ahora sòlo falta que todo el mundo pueda admirarla. Si alguien desea esta foto para su colecciòn personal, gustoso podemos negociar los terminos para la licencia. Y si creen que puede ganar algùn concurso, por favor màndenme un mail y estarè muy agradecido.

Sunday, October 23, 2005

Welcome to my new blog! Here I'll try to publish my thoughts and expreiences about one of my hobbies, putting things apart (mainly electronics), fixing stuff, etc.

My goal is to become a reference for people wanting to be their own repair technicians but are too scared or doubtful to begin. It's true, you can screw hard and make things not work anymore, but my motto is: If it's already broken, let's try to fix it (There's nothing to loose :)

Finally, a little disclaimer. Of course, these are only my experiences. I can't guarantee you will have the same results. And any damage you make to your stuff or yourself is entirely your responsiblity. And last but not least, always apply general common sense and play safe.