Using the Amazon Product Advertising API in PHP

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:

  • ecs.amazonaws.com: based on the country you want to target, you have to use a specific endpoint. It is ecs.amazonaws.com for USA.
  • AWSAccessKeyId: you get this access key in your Amazon Web Services account.
  • ItemPage: for the first request you set this to 1 and increase it in previous requests if you need more items. Each response has up to 10 items (you cannot change that).
  • Keywords: your search keywords. Make sure you use rawurlencode here.
  • Operation: the name of the operation of the Amazon Product Advertising API you want to call. For the keyword search we choose ItemSearch.
  • ResponseGroup: based on the level of detail you need in the response, choose a useful response group. For item searches I choose Medium most of the time.
  • SearchIndex: a search index is a kind of first level category. If you want to search in everything, just choose All.
  • Service: just use AWSECommerceService here.
  • Timestamp:a timestamp generated in PHP like this:
    rawurlencode(gmdate('Y-m-d\TH:i:s\Z'))
  • Signature: that’s the funniest part: please see below how to generate this signature.

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?

This post is also available in Deutsch.

2 thoughts on “Using the Amazon Product Advertising API in PHP

  1. Pingback: Using the eBay Finding API in PHP | AB-WebLog.com

  2. Pingback: Using the Shopping.com API in PHP | AB-WebLog.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>