As I have started with an example of the eBay Finding API in a previous post, I want to continue with the Amazon Product Advertising API now.
Again we need to start with a sign up for a free Amazon Web Services account. To make money with your Amazon traffic you also need to register for the Amazon affiliate program.
Now we can start to build the request URL for the Amazon Product Advertising API:
http://ecs.amazonaws.com/onca/xml?
AWSAccessKeyId=%awsAccessKeyId%&
AssociateTag=%associateTag%&
ItemPage=%itemPage%&
Keywords=%keywords%&
Operation=%operation%&
ResponseGroup=%responseGroup%&
SearchIndex=%categoryId%&
Service=AWSECommerceService&
Timestamp=%timestamp%&
Signature=%signature%
Let’s see what the individual parameters mean:
To be able to send a valid API request, you have to calculate the signature as following:
$request = "GET\necs.amazonaws.com\n/onca/xml\n";
$request .= "AWSAccessKeyId=%awsAccessKeyId%&AssociateTag=%associateTag%&ItemPage=%itemPage%&Keywords=%keywords%&Operation=%operation%&ResponseGroup=%responseGroup%&SearchIndex=%categoryId%&Service=AWSECommerceService&Timestamp=%timestamp%";
$signature = rawurlencode(base64_encode(hash_hmac('sha256', $request, %secretKey%, true)));
You get the needed %secretKey% in your Amazon Web Services account, too.
When you’ve done everything correctly, you can pass the resulting request URL to this small function that we used already for the eBay Finding API request and receive the XML document response of the API:
/**
* Returns the SimpleXML object.
*
* @param string $url The URL to receive the XML document.
* @return string The SimpleXML object.
*
*/
function getXml($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$result = curl_exec($ch);
curl_close($ch);
return simplexml_load_string($result);
}
Unfortunately I have to say that I think the Amazon Product Advertising API is really confusing and badly documented. Especially if you need to work with these search indices and categories (so-called nodes) that’s not very funny at all. And there are even things that are not bad, but just wrong documented in the Amazon Product Advertising API documentation.
What’s your opinion about the Amazon Product Advertising API?