View Source


<ac:macro ac:name="toc"><ac:parameter ac:name="style">none</ac:parameter><ac:parameter ac:name="indent">25px</ac:parameter></ac:macro>


<h1>Zend_Service_Amazon</h1>


<h2>Introduction</h2>
<p> <em>Zend_Service_Amazon</em> is a simple API for using Amazon web services. <em>Zend_Service_Amazon</em> has two APIs: a more traditional one that follows Amazon's own API, and a simpler &quot;Query API&quot; for constructing even complex search queries easily.
<br class="atl-forced-newline" /></p>

<p> <em>Zend_Service_Amazon</em> enables developers to retrieve information appearing throughout Amazon.com web sites directly through the Amazon Web Services API. Examples include: </p>
<ul>
<li>Store item information, such as images, descriptions, pricing, and more</li>
<li>Customer and editorial reviews</li>
<li>Similar products and accessories</li>
<li>Amazon.com offers</li>
<li>ListMania lists
<br class="atl-forced-newline" /></li>
</ul>


<p> In order to use <em>Zend_Service_Amazon</em>, you should already have an Amazon developer API key. To get a key and for more information, please visit the Amazon Web Services web site.
<br class="atl-forced-newline" /></p>



<ac:macro ac:name="note"><ac:rich-text-body>
<p> Your Amazon developer API key is linked to your Amazon identity, so take appropriate measures to keep your API key private. </p></ac:rich-text-body></ac:macro>




<p><strong>Search Amazon Using the Traditional API</strong></p>


<p> In this example, we search for PHP books at Amazon and loop through the results, printing them.
<br class="atl-forced-newline" /></p>


<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
require_once 'Zend/Service/Amazon.php';
$amazon = new Zend_Service_Amazon('AMAZON_API_KEY&');
$response = $amazon->itemSearch(array('SearchIndex' => 'Books', 'Keywords' => 'php'));
foreach ($response as $r) {
echo $r->Title .'<br />';
}
?>
]]></ac:plain-text-body></ac:macro>




<p><strong>Search Amazon Using the Query API</strong></p>


<p> Here, we also search for PHP books at Amazon, but we instead use the Query API, which resembles the Fluent Interface design pattern.
<br class="atl-forced-newline" /></p>


<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
require_once 'Zend/Service/Amazon/Query.php';
$query = new Zend_Service_Amazon_Query('AMAZON_API_KEY');
$query->category('Books')->Keywords('PHP');
$results = $query->search();
foreach ($results as $result) {
echo $result->Title .'<br />';
}
?>
]]></ac:plain-text-body></ac:macro>





<h2>Country Codes</h2>
<p> By default, <em>Zend_Service_Amazon</em> connects to the United States (&quot;<em>US</em>&quot;) Amazon web service. To connect from a different country, simply specify the appropriate country code string as the second parameter to the constructor:
<br class="atl-forced-newline" /></p>



<p><strong>Choosing an Amazon Web Service Country</strong></p>



<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
// Connect to Amazon in Japan
require_once 'Zend/Service/Amazon.php';
$amazon = new Zend_Service_Amazon('AMAZON_API_KEY', 'JP');
?>
]]></ac:plain-text-body></ac:macro>



<ac:macro ac:name="note"><ac:rich-text-body>
<p> Valid country codes are: CA, DE, FR, JP, UK, and US. </p></ac:rich-text-body></ac:macro>






<h2>Looking up a Specific Amazon Item by ASIN</h2>
<p> The <em>itemLookup()</em> method provides the ability to fetch a particular Amazon item when the ASIN is known.
<br class="atl-forced-newline" /></p>



<p><strong>Looking up a Specific Amazon Item by ASIN</strong></p>



<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
require_once 'Zend/Service/Amazon.php';
$amazon = new Zend_Service_Amazon('AMAZON_API_KEY');
$item = $amazon->itemLookup('B0000A432X');
?>
]]></ac:plain-text-body></ac:macro>


<p> The <em>itemLookup()</em> method also accepts an optional second parameter for handling search options. For full details, including a list of available options, please see the relevant Amazon documentation.
<br class="atl-forced-newline" /></p>




<h2>Performing Amazon Item Searches</h2>
<p> Searching for items based on any of various available criteria are made simple using the <em>itemSearch()</em> method, as in the following example:
<br class="atl-forced-newline" /></p>



<p><strong>Performing Amazon Item Searches</strong></p>



<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
require_once 'Zend/Service/Amazon.php';
$amazon = new Zend_Service_Amazon('AMAZON_API_KEY');
$response = $amazon->itemSearch(array('SearchIndex' => 'Books', 'Keywords' => 'php'));
foreach($response as $r) {
echo $r->Title .'<br />';
}
?>
]]></ac:plain-text-body></ac:macro>


<p> The <em>itemSearch()</em> method accepts a single array parameter for handling search options. For full details, including a list of available options, please see the relevant Amazon documentation
<br class="atl-forced-newline" /></p>


<ac:macro ac:name="tip"><ac:rich-text-body>
<p> The Zend_Service_Amazon_Query class is an easy to use wrapper around this method. </p></ac:rich-text-body></ac:macro>




<h2>Using the Alternative Query API</h2>


<h3>Introduction</h3>
<p> <em>Zend_Service_Amazon_Query</em> provides an alternative API for using the Amazon Web Service. The alternative API uses the Fluent Interface pattern. That is, all calls can be made using chained method calls. (e.g., <em>$obj-&gt;method()-&gt;method2($arg)</em>)
<br class="atl-forced-newline" /></p>

<p> The <em>Zend_Service_Amazon_Query</em> API uses overloading to easily set up an item search and then allows you to search based upon the criteria specified. Each of the options is provided as a method call, and each method's argument corresponds to the named option's value:
<br class="atl-forced-newline" /></p>



<p><strong>Search Amazon Using the Alternative Query API</strong></p>


<p> In this example, the alternative query API is used as a fluent interface to specify options and their respective values:
<br class="atl-forced-newline" /></p>


<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
require_once 'Zend/Service/Amazon/Query.php';
$query = new Zend_Service_Amazon_Query('MY_API_KEY');
$query->Category('Books')->Keywords('PHP');
$results = $query->search();
foreach ($results as $result) {
echo $result->Title .'<br />';
}
?>
]]></ac:plain-text-body></ac:macro>

<p> This sets the option <em>Category</em> to &quot;Books&quot; and <em>Keywords</em> to &quot;PHP&quot;.
<br class="atl-forced-newline" /></p>

<p> For more information on the available options, please refer to the relevant Amazon documentation.
<br class="atl-forced-newline" /></p>


<ac:macro ac:name="toc-zone" />