<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AB-WebLog.com&#187; Amazon</title>
	<atom:link href="http://www.ab-weblog.com/en/tag/amazon/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ab-weblog.com/en</link>
	<description>Andreas Breitschopp</description>
	<lastBuildDate>Wed, 18 Mar 2015 09:47:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using the Amazon Product Advertising API in PHP</title>
		<link>http://www.ab-weblog.com/en/using-the-amazon-product-advertising-api-in-php/</link>
		<comments>http://www.ab-weblog.com/en/using-the-amazon-product-advertising-api-in-php/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 14:04:46 +0000</pubDate>
		<dc:creator>Andreas Breitschopp</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.ab-weblog.com/en/?p=172</guid>
		<description><![CDATA[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 &#8230; <a href="http://www.ab-weblog.com/en/using-the-amazon-product-advertising-api-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As I have started with an example of the <a title="Using the eBay Finding API in PHP" href="http://www.ab-weblog.com/en/using-the-ebay-finding-api-in-php/">eBay Finding API</a> in a previous post, I want to continue with the Amazon Product Advertising API now.</p>
<p>Again we need to start with a sign up for a free <a title="Amazon Web Services Website" href="http://aws.amazon.com" target="_blank">Amazon Web Services account</a>. To make money with your Amazon traffic you also need to register for the <a title="Amazon Affiliate Program Website" href="https://affiliate-program.amazon.com" target="_blank">Amazon affiliate program</a>.</p>
<p>Now we can start to build the request URL for the Amazon Product Advertising API:</p>
<pre class="brush: text; gutter: true">http://ecs.amazonaws.com/onca/xml?
	AWSAccessKeyId=%awsAccessKeyId%&amp;
	AssociateTag=%associateTag%&amp;
	ItemPage=%itemPage%&amp;
	Keywords=%keywords%&amp;
	Operation=%operation%&amp;
	ResponseGroup=%responseGroup%&amp;
	SearchIndex=%categoryId%&amp;
	Service=AWSECommerceService&amp;
	Timestamp=%timestamp%&amp;
	Signature=%signature%</pre>
<p>Let&#8217;s see what the individual parameters mean:</p>
<ul>
<li><em>ecs.amazonaws.com:</em> based on the country you want to target, you have to use a <a title="List of Possible Endpoints" href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?AnatomyOfaRESTRequest.html" target="_blank">specific endpoint</a>. It is <em>ecs.amazonaws.com</em> for USA.</li>
<li><em>AWSAccessKeyId:</em> you get this access key in your Amazon Web Services account.</li>
<li><em>ItemPage:</em> for the first request you set this to <em>1</em> and increase it in previous requests if you need more items. Each response has up to 10 items (you cannot change that).</li>
<li><em>Keywords:</em> your search keywords. Make sure you use <em>rawurlencode</em> here.</li>
<li><em>Operation:</em> the name of the operation of the Amazon Product Advertising API you want to call. For the keyword search we choose <em><a title="More Info About the Operation ItemSearch" href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/ItemSearch.html" target="_blank">ItemSearch</a></em>.</li>
<li><em>ResponseGroup:</em> based on the level of detail you need in the response, choose a useful <a title="List of Response Groups" href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html" target="_blank">response group</a>. For item searches I choose <em>Medium</em> most of the time.</li>
<li><em>SearchIndex:</em> a <a title="List of Search Indices" href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?APPNDX_SearchIndexValues.html" target="_blank">search index</a> is a kind of first level category. If you want to search in everything, just choose <em>All</em>.</li>
<li><em>Service:</em> just use <em>AWSECommerceService</em> here.</li>
<li><em>Timestamp:</em>a timestamp generated in PHP like this:
<pre class="brush: php; gutter: false">rawurlencode(gmdate('Y-m-d\TH:i:s\Z'))</pre>
</li>
<li><em>Signature:</em> that&#8217;s the funniest part: please see below how to generate this signature.</li>
</ul>
<p>To be able to send a valid API request, you have to calculate the signature as following:</p>
<pre class="brush: php; gutter: true">$request = "GET\necs.amazonaws.com\n/onca/xml\n";
$request .= "AWSAccessKeyId=%awsAccessKeyId%&amp;AssociateTag=%associateTag%&amp;ItemPage=%itemPage%&amp;Keywords=%keywords%&amp;Operation=%operation%&amp;ResponseGroup=%responseGroup%&amp;SearchIndex=%categoryId%&amp;Service=AWSECommerceService&amp;Timestamp=%timestamp%";
$signature = rawurlencode(base64_encode(hash_hmac('sha256', $request, %secretKey%, true)));</pre>
<p>You get the needed <em>%secretKey%</em> in your Amazon Web Services account, too.</p>
<p>When you&#8217;ve done everything correctly, you can pass the resulting request URL to this small function that we used already for the <a title="Using the eBay Finding API in PHP" href="http://www.ab-weblog.com/en/using-the-ebay-finding-api-in-php/">eBay Finding API</a> request and receive the XML document response of the API:</p>
<pre class="brush: php; gutter: true">/**
 * 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);
}</pre>
<p>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 <em>nodes</em>) that&#8217;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.</p>
<p><em>What&#8217;s your opinion about the Amazon Product Advertising API?</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ab-weblog.com/en/using-the-amazon-product-advertising-api-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->