<?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>code-diesel &#187; amazon</title>
	<atom:link href="http://www.codediesel.com/tag/amazon/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Amazon Advertising API BrowseNodes</title>
		<link>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/</link>
		<comments>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 16:25:35 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[amazon]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2978</guid>
		<description><![CDATA[Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items, such as Harry Potter books, not the items themselves. Browse node IDs are positive integers that uniquely identify product collections, for example, Literature &#038; Fiction: (17), Medicine: (13996), Mystery &#038; Thrillers: (18), Nonfiction: (53), Outdoors &#038; Nature: [...]]]></description>
			<content:encoded><![CDATA[<p>Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items, such as Harry Potter books, not the items themselves. Browse node IDs are positive integers that uniquely identify product collections, for example, Literature &#038; Fiction: (17), Medicine: (13996), Mystery &#038; Thrillers: (18), Nonfiction: (53), Outdoors &#038; Nature: (290060). Amazon uses over 120,000 browse node IDs in the US locale alone.</p>
<p>The BrowseNodes tool automatically gets all the child BrowseNodes and their names. The program recursively traverses the BrowserNode hierarchy and returns all the BrowseNodes and their respective names. You can display the nodes on the console or save it to a CSV file. You can also include the library in your existing projects to process BrowseNodes.<br />
<span id="more-2978"></span></p>
<h4>Library Usage</h4>
<p>The library is easy to use, and requires PHP and CURL installed on your system. The download includes a sample file with which you can immediately test the code. The sample file is shown below.</p>

<div class="wp_codebox"><table><tr id="p297811"><td class="code" id="p2978code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the BrowseNodes to the console */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Before you begin to use the library, you need to enter your Amazon security credentials as shown above. Also in the &#8216;aws_signed_request.php&#8217; file you will need to enter your Amazon Associate ID, as it is now mandatory. Find the following line in the file and add your Associated ID.</p>

<div class="wp_codebox"><table><tr id="p297812"><td class="code" id="p2978code12"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* aws_signed_request.php */</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;AssociateTag&quot;</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR-ASSOCIATES-ID-HERE'</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>That is it. Now you are ready to get the BrowserNodes from Amazon. You can display the BrowserNodes on you console/browser or save it directly to a CSV file. For that you have to use the &#8216;setMedia&#8217; method. Both example are shown below.</p>
<h4>Display the BrowseNodes on the console</h4>

<div class="wp_codebox"><table><tr id="p297813"><td class="code" id="p2978code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the BrowseNodes to the console */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You enter the parent browsenode in the &#8216;getBrowseNodes&#8217; method, after which the function recursively retrieves all the children nodes. To get started, you can find all the top browsenode values <a href="http://affiliate-program.amazon.com/gp/redirect.html/ref=amb_link_7285822_2?location=http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/BrowseNodeIDs.html&#038;token=948F5A3F73BA8D32EB91AADB246FBC94772F93D8&#038;pf_rd_m=ATVPDKIKX0DER&#038;pf_rd_s=assoc-center-1&#038;pf_rd_r=&#038;pf_rd_t=501&#038;pf_rd_p=&#038;pf_rd_i=assoc_help_t41_a6" target="_blank">here</a>, or you can get it from the Amazon link itself.</p>
<p>The output of the above is shown below. The output is indented so that you can visualize the hierarchy of the nodes.</p>

<div class="wp_codebox"><table><tr id="p297814"><td class="code" id="p2978code14"><pre class="text" style="font-family:monospace;">Television &amp; Video : AV Receivers &amp; Amplifiers : 3213035011
Television &amp; Video : Blu-ray Players &amp; Recorders : 3213025011
      Blu-ray Players &amp; Recorders : Blu-ray Recorders : 3213026011
      Blu-ray Players &amp; Recorders : Blu-ray Players : 352697011
Television &amp; Video : DVD Players &amp; Recorders : 3213027011
      DVD Players &amp; Recorders : DVD Players : 1036922
      DVD Players &amp; Recorders : DVD Recorders : 1036920
Television &amp; Video : DVD-VCR Combos : 886258
Television &amp; Video : HD DVD Players : 352696011
Television &amp; Video : Televisions : 172659
Television &amp; Video : Home Theater Systems : 281056
.
.</pre></td></tr></table></div>

<p>The format of each line is given below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/12/browsenode.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/browsenode.gif" alt="" title="amazon advertising browsenode" width="436" height="88" class="aligncenter size-full wp-image-2958" /></a></p>
<h4>Saving the BrowseNodes to a CSV</h4>
<p>The following code shows how to save the browsenodes to a CSV file.</p>

<div class="wp_codebox"><table><tr id="p297815"><td class="code" id="p2978code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Write the BrowseNodes to a CSV file */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;csv&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;./nodes.csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The code saves all the browsenodes in a CSV file, after which you can use it with Excel or a simple text editor. A sample CSV ouput is shown below. The format is the same as the one given above.</p>

<div class="wp_codebox"><table><tr id="p297816"><td class="code" id="p2978code16"><pre class="text" style="font-family:monospace;">&quot;PlayStation 3&quot;,&quot;All Games&quot;,&quot;14210861&quot;
&quot;PlayStation 3&quot;,&quot;Action&quot;,&quot;14210761&quot;
&quot;Action&quot;,&quot;Fighting&quot;,&quot;14210981&quot;
&quot;Action&quot;,&quot;Military &amp; Espionage&quot;,&quot;14210821&quot;
&quot;Action&quot;,&quot;Shooter&quot;,&quot;14210841&quot;
&quot;PlayStation 3&quot;,&quot;Adventure&quot;,&quot;14210851&quot;
&quot;PlayStation 3&quot;,&quot;Arcade&quot;,&quot;14210911&quot;
&quot;PlayStation 3&quot;,&quot;Board Games&quot;,&quot;14210921&quot;</pre></td></tr></table></div>

<h4>Changing Locales</h4>
<p>Amazon being a world-wide venture the Product Advertising API operates in the following locales:<br />
    CA<br />
    CN<br />
    DE<br />
    ES<br />
    FR<br />
    IT<br />
    JP<br />
    UK<br />
    US</p>
<p>You can use different locale code to get the BrowseNodes for different regions. You set the locale parameter using the &#8216;$region&#8217; variable; for example:</p>

<div class="wp_codebox"><table><tr id="p297817"><td class="code" id="p2978code17"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$region</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span></pre></td></tr></table></div>

<h4>Getting the parent node details</h4>
<p>You can also get information about a nodes parent node using the &#8216;getParentNode&#8217; method.</p>

<div class="wp_codebox"><table><tr id="p297818"><td class="code" id="p2978code18"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParentNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;14210821&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>One other useful method you can use it &#8216;getNodeName&#8217;. This allows you to get the name of a given node id. This can be useful if you have a nodeid but need to gets it name.</p>

<div class="wp_codebox"><table><tr id="p297819"><td class="code" id="p2978code19"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNodeName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;14210821&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Note</strong>: Some categories contain hundreds to thousands of BrowserNodes, retreiving them can take some time and could trigger a PHP timeout error. To avoid this set PHP timeout to a large number as given below.</p>

<div class="wp_codebox"><table><tr id="p297820"><td class="code" id="p2978code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div  class="download2">
<a href="http://www.codediesel.com/downloads/amazon-bnodes">Download code files</a><br />
<span>Downloads : 172  / File size : 3.1 kB</span>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Accessing Amazon Product Advertising API</title>
		<link>http://www.codediesel.com/php/accessing-amazon-product-advertising-api-in-php/</link>
		<comments>http://www.codediesel.com/php/accessing-amazon-product-advertising-api-in-php/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 05:39:49 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[api]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1457</guid>
		<description><![CDATA[amazon product advertising api tutorial]]></description>
			<content:encoded><![CDATA[<p><strong>Updated: 19th October 2011</strong></p>
<p>In this post we will see how to access the Amazon Product Advertising API from PHP. Amazon has recently changed (from 15th Aug &#8217;09) the authentication mechanism for accessing their API which must now be signed with your Amazon keys. Unsigned requests will be rejected by Amazon. Also now Amazon Associate Tag is required in the query (effective from 25th Oct. 2011). Note that the code uses the hash_hmac() hash function which is only available for PHP versions 5.1.2 and above, so the code will not work for versions below that.<br />
<span id="more-1457"></span></p>
<h4>A small example</h4>
<p>Below is an example to access the Amazon Product Advertising API using the provided class.</p>

<div class="wp_codebox"><table><tr id="p145721"><td class="code" id="p1457code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
    <span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    try
    <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* Returns a SimpleXML object */</span>
         <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">searchProducts</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;X-Men Origins&quot;</span><span style="color: #339933;">,</span>
                                       AmazonProductAPI<span style="color: #339933;">::</span><span style="color: #004000;">DVD</span><span style="color: #339933;">,</span>
                                       <span style="color: #0000ff;">&quot;TITLE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>The API access class</h4>
<p>Given below is the implementation of the class to access the Amazon Product Advertising API. Comments have been removed for brevity, but are included in the source download. Note that only a few access operations are implemented in the class: <em>ItemLookup</em>, <em>ItemSearch</em>; there are more available in the API, a complete list can be found <a target="_blank" href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/CHAP_OperationListAlphabetical.html">here</a>. It&#8217;s just a simple matter of changing some parameters to implement others.</p>

<div class="wp_codebox"><table><tr id="p145722"><td class="code" id="p1457code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'aws_signed_request.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> AmazonProductAPI
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR AMAZON ACCESS KEY ID&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR AMAZON SECRET KEY&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* 'Associate Tag' now required, effective from 25th Oct. 2011 */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$associate_tag</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR AMAZON ASSOCIATE TAG&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">const</span> MUSIC <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Music&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">const</span> DVD   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DVD&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">const</span> GAMES <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;VideoGames&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> verifyXmlResponse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">False</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not connect to Amazon&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Items</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ItemAttributes</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Title</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Invalid xml response.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> queryAmazon<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameters</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> aws_signed_request<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">,</span>
                                  <span style="color: #000088;">$parameters</span><span style="color: #339933;">,</span>
                                  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">public_key</span><span style="color: #339933;">,</span>
                                  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">private_key</span><span style="color: #339933;">,</span>
                                  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">associate_tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> searchProducts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search</span><span style="color: #339933;">,</span><span style="color: #000088;">$category</span><span style="color: #339933;">,</span><span style="color: #000088;">$searchType</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UPC&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$allowedTypes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPC&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;TITLE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ARTIST&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;KEYWORD&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$allowedCategories</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Music&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;DVD&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;VideoGames&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$searchType</span><span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;UPC&quot;</span> <span style="color: #339933;">:</span>
                <span style="color: #000088;">$parameters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Operation&quot;</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ItemLookup&quot;</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">&quot;ItemId&quot;</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$search</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">&quot;SearchIndex&quot;</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$category</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">&quot;IdType&quot;</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;UPC&quot;</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">&quot;ResponseGroup&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Medium&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;TITLE&quot;</span> <span style="color: #339933;">:</span>
                <span style="color: #000088;">$parameters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Operation&quot;</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ItemSearch&quot;</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">&quot;Title&quot;</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$search</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">&quot;SearchIndex&quot;</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$category</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">&quot;ResponseGroup&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Medium&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$xml_response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryAmazon</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verifyXmlResponse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getItemByUpc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$upc_code</span><span style="color: #339933;">,</span> <span style="color: #000088;">$product_type</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$parameters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Operation&quot;</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ItemLookup&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;ItemId&quot;</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$upc_code</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;SearchIndex&quot;</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$product_type</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;IdType&quot;</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;UPC&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;ResponseGroup&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Medium&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$xml_response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryAmazon</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verifyXmlResponse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getItemByAsin<span style="color: #009900;">&#40;</span><span style="color: #000088;">$asin_code</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$parameters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Operation&quot;</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ItemLookup&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;ItemId&quot;</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$asin_code</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;ResponseGroup&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Medium&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$xml_response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryAmazon</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verifyXmlResponse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getItemByKeyword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$keyword</span><span style="color: #339933;">,</span> <span style="color: #000088;">$product_type</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$parameters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Operation&quot;</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ItemSearch&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;Keywords&quot;</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$keyword</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">&quot;SearchIndex&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$product_type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$xml_response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryAmazon</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verifyXmlResponse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Amazon signed request</h4>
<p>The above class uses the &#8216;aws_signed_request&#8217; function to generate the new request signature. Original code is by <a target="_blank" href="http://mierendo.com/">Ulrich Mierendorff</a>, modified here to use cURL.</p>

<div class="wp_codebox"><table><tr id="p145723"><td class="code" id="p1457code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span>  aws_signed_request<span style="color: #009900;">&#40;</span><span style="color: #000088;">$region</span><span style="color: #339933;">,</span>
                             <span style="color: #000088;">$params</span><span style="color: #339933;">,</span>
                             <span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span>
                             <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span>
                             <span style="color: #000088;">$associate_tag</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ecs.amazonaws.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$region</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$uri</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/onca/xml&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Service&quot;</span><span style="color: #009900;">&#93;</span>          <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AWSECommerceService&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;AWSAccessKeyId&quot;</span><span style="color: #009900;">&#93;</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$public_key</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;AssociateTag&quot;</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$associate_tag</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Timestamp&quot;</span><span style="color: #009900;">&#93;</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d<span style="color: #000099; font-weight: bold;">\T</span>H:i:s\Z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Version&quot;</span><span style="color: #009900;">&#93;</span>          <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2009-03-31&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* The params need to be sorted by the key, as Amazon does this at
      their end and then generates the hash of the same. If the params
      are not in order then the generated hash will be different from
      Amazon thus failing the authentication process.
    */</span>
    <span style="color: #990000;">ksort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$canonicalized_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$param</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$param</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%7E&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;~&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rawurlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%7E&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;~&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rawurlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$canonicalized_query</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$param</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$canonicalized_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$canonicalized_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$string_to_sign</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$method</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$host</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$uri</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span>
                            <span style="color: #000088;">$canonicalized_query</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* calculate the signature using HMAC, SHA256 and base64-encoding */</span>
    <span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">hash_hmac</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sha256&quot;</span><span style="color: #339933;">,</span> 
                                  <span style="color: #000088;">$string_to_sign</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">True</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* encode the signature for the request */</span>
    <span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%7E&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;~&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rawurlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* create request */</span>
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$host</span><span style="color: #339933;">.</span><span style="color: #000088;">$uri</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;?&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$canonicalized_query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;Signature=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$signature</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* I prefer using CURL */</span>
    <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_SSL_VERIFYHOST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$xml_response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_response</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">False</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900; font-weight: bold;">False</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* parse XML and return a SimpleXML object, if you would
           rather like raw xml then just return the $xml_response.
         */</span>
        <span style="color: #000088;">$parsed_xml</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$parsed_xml</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">False</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">False</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$parsed_xml</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div  class="download2">
<a href="http://www.codediesel.com/downloads/amazonapi">Download Source</a><br />
<span>Downloads : 4891  / File size : 5 kB</span>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/accessing-amazon-product-advertising-api-in-php/feed/</wfw:commentRss>
		<slash:comments>63</slash:comments>
		</item>
	</channel>
</rss>

