How to Iterate through Amazon Product Results

The Amazon Product Advertising API is designed to only provide 10 product results for any request. This is done to reduce the traffic load on the server as in most cases you will not need to obtain any additional products beyond them. The API informs you how many total results matched your query by the TotalResults parameter and to obtain the rest of the results you need to utilize the ItemPage parameter to specify which page of the results are to be return.

Basic Paging of Product Results

// Setup and Configure API
$api = new AmazonProductAPI();
... //set public,private, and associate key/id

// Setup Request
$request = new AmazonProduct_Request();
$request->Operation = AmazonProduct_Operation::ITEM_SEARCH;
$request->Title = "Towers of Midnight";
$request->ItemPage = 2;

// Send Request
$response = $api->search( $request );

// Iterate through returned Items
if( $response->isSuccess() ) {
    foreach( $response->Items as $item ) {
        //consume Item
        echo $item->ASIN;
    }
}

The preceding example shows a brief snippet of how you can specify the results page to return. To process all the search results would be even more involved as you would need to test if there are additional results/pages to get. Luckily we have taken this into account when building the APaPi library as we want to make it as easy as possible for you to interact with the The Amazon Product Advertising API, that is why we created the AmazonProduct_Iterator which implements PHP’s core interface Iterator.

You don’t need to understand the full implementation details of Iterators to utilize the APaPi library, instead all you need to know is that the AmazonProduct_Result object implements the getIterator method that can be used in a foreach method to loop through all the results for a search against the The Amazon Product Advertising API. This convenience method allows you to not worry about handling paging of the results as the AmazonProduct_Iterator will handle that all behind the scenes by querying the The Amazon Product Advertising API as needed.

Iterating through Product Results

// Setup and Configure API
$api = new AmazonProductAPI();
... //set public,private, and associate key/id

// Setup Request
$request = new AmazonProduct_Request();
$request->Operation = AmazonProduct_Operation::ITEM_SEARCH;
$request->Title = "Towers of Midnight";

// Send Request
$response = $api->search( $request );

// Iterate through returned Items
if( $response->isSuccess() ) {
    foreach( $response->getIterator() as $item ) {
        //consume Item
        echo $item->ASIN;
    }
}

As seen from the above example snippet the APaPi library strives to make utilizing the The Amazon Product Advertising API as simple and user friendly as possible. Although it may not be used 90% of the time this simple convenience method lets you not worry about how many subsequent requests need to be made instead you can simply treat it as an array to be looped through.

Comments & Questions

  1. Was having an issue with the iterator on PHP 5.3.3 on Mac OS X 10.6.6. Had to modify iterator.php, line 65 from: if( is_int( $result->TotalResults ) ) to: if( is_int( intval($result->TotalResults) ) ).

    Seems is_int($result->TotalResults) was always returning false.

    Very nice piece of work here.

    1. Thanks for informing me of this issue. I’ll update the code so that no other users have the same problem.

      > Matthew

Add Your Comment