Sep posted 13 Sep 2009 and tagged php
Amazon recently changed its Product API requirements and it was a little tricky to get the new signature working, so here’s an example of a product search in PHP.
function _encode_query($s) {
$s = str_replace(',', urlencode(','), $s);
$s = str_replace(':', urlencode(':'), $s);
$s = str_replace(';', urlencode(';'), $s);
return $s;
}
function _encode_signature($s) {
$s = str_replace('+', urlencode('+'), $s);
$s = str_replace('=', urlencode('='), $s);
return $s;
}
function fetch($power_query, $access_key_id, $associate_tag) {
$url_base = 'http://webservices.amazon.com/onca/xml?';
$url_params = array(
'Operation' => 'ItemSearch',
'Service' => 'AWSECommerceService',
'AWSAccessKeyId' => $access_key_id,
'Version' => '2009-01-06',
'Sort' => 'salesrank',
'ResponseGroup' => 'ItemAttributes,Images',
'SearchIndex' => 'Books',
'Power' => $power_query,
'Availability' => 'Available',
'Condition' => 'All',
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z')
);
$pairs = array();
foreach($url_params as $k => $v) {
$pairs[] = $k.'='.$this->_encode_query($v);
}
sort($pairs);
$canonical = implode('&', $pairs);
$base = 'GET'.PHP_EOL.'webservices.amazon.com'.PHP_EOL.'/onca/xml'.PHP_EOL;
$to_sign = $base.$canonical;
$signature = base64_encode(hash_hmac('sha256', $to_sign, $associate_tag, true));
$signature = $this->_encode_signature($signature);
$request = $url_base.$canonical.'&Signature='.$signature;
$response = file_get_contents($request);
$xml = simplexml_load_string($response);
if (!is_object($xml)) {
return null;
} else {
$products = array();
if (sizeof($xml->Items->Item) > 0) {
foreach($xml->Items->Item as $item) {
$products[] = $item;
}
}
return $products;
}
}
Pop out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | function _encode_query($s) { $s = str_replace(',', urlencode(','), $s); $s = str_replace(':', urlencode(':'), $s); $s = str_replace(';', urlencode(';'), $s); return $s; } function _encode_signature($s) { $s = str_replace('+', urlencode('+'), $s); $s = str_replace('=', urlencode('='), $s); return $s; } function fetch($power_query, $access_key_id, $associate_tag) { $url_base = 'http://webservices.amazon.com/onca/xml?'; $url_params = array( 'Operation' => 'ItemSearch', 'Service' => 'AWSECommerceService', 'AWSAccessKeyId' => $access_key_id, 'Version' => '2009-01-06', 'Sort' => 'salesrank', 'ResponseGroup' => 'ItemAttributes,Images', 'SearchIndex' => 'Books', 'Power' => $power_query, 'Availability' => 'Available', 'Condition' => 'All', 'Timestamp' => gmdate('Y-m-d\TH:i:s\Z') ); $pairs = array(); foreach($url_params as $k => $v) { $pairs[] = $k.'='.$this->_encode_query($v); } sort($pairs); $canonical = implode('&', $pairs); $base = 'GET'.PHP_EOL.'webservices.amazon.com'.PHP_EOL.'/onca/xml'.PHP_EOL; $to_sign = $base.$canonical; $signature = base64_encode(hash_hmac('sha256', $to_sign, $associate_tag, true)); $signature = $this->_encode_signature($signature); $request = $url_base.$canonical.'&Signature='.$signature; $response = file_get_contents($request); $xml = simplexml_load_string($response); if (!is_object($xml)) { return null; } else { $products = array(); if (sizeof($xml->Items->Item) > 0) { foreach($xml->Items->Item as $item) { $products[] = $item; } } return $products; } } |