Searching for Similar Products

Let's go into more advanced use of Amazon AWS by returning similar products. Refer to Table 7-8 to view which key allows you to fetch similar products. Using the ResponseGroup key and specifying the Similarities value, the result set will return an array of Zend_Service_Amazon_SimilarProduct objects. Each object contains the attributes outlined in Table 7-11.

Table 7-11. Zend_Service_Amazon_SimilarProduct Object Attributes




Unique Amazon ID for the product


Title of the product

The power of the Zend_Service_Amazon library is that you need only two operations to make a call to fetch product information, and the only thing you have to worry about is which parameter fetches the proper data for you and how you can display the data. Listing 7-22 demonstrates this by calling similar products for you. You use the ResponseGroup key and the Small value, which is the default ResponseGroup value and fetches all the standard data, and Similarities to return the similar titles. With the result set fetched, you now have the Zend_Service_Amazon_ResultSet's SimilarProducts attribute. The attribute contains a collection of Zend_Service_Amanzon_SimilarProducts objects that can be accessed in a loop.

Listing 7-22. Fetching Similar Products public function amazonSimilarProductsTestAction() {


$amazon = new Zend_Service_Amazon('API_KEY', 'US');

$results = $amazon->itemSearch(array('SearchIndex' => 'Books', 'Keywords' => 'PHP', 'Condition' => 'Used', 'Publisher' => 'Apress', 'Sort' => 'titlerank', 'ItemPage' => '3', 'ResponseGroup' => 'Small,Similarities'));

//Foreach item return the Similar Products foreach($results as $result){ echo "<b>".$result->Title."</b><br>";

$similarProduct = $result->SimilarProducts;

if(empty($similarProduct)) {

echo "No recommendations."; } else {

foreach($similarProduct as $similar){ echo "Recommended Books: ".$similar->Title."<br>";

}catch(Zend_Exception $e){ throw $e; } echo "<br>";

echo "Total Books: ".$results->totalResults(); echo "<br>";

echo "Total Pages: ".$results->totalPages(); $this->_helper->viewRenderer->setNoRender();

Was this article helpful?

0 0

Post a comment