ZF-9547: Handle 'empty' Amazon book search results


I'm using Zend_Service_Amazon to display book information, using itemSearch() to search by ISBN. Every now and then, Amazon seems to return a valid result without any data for these queries, which causes the following fatal PHP error from ZF:

Catchable fatal error: Argument 1 passed to Zend_Service_Amazon_Item::__construct() must be an instance of DOMElement, null given, called in /home/tim/zend-framework/ZendFramework-1.10.1/library/Zend/Service/Amazon/ResultSet.php on line 111 and defined in /home/tim/zend-framework/ZendFramework-1.10.1/library/Zend/Service/Amazon/Item.php on line 118

Here's an example script, you'll need to add valid API key data:

$isbn = '0750640162'; // doesn't work
//$isbn = '0754512673'; // does work

$client = new Zend_Service_Amazon('AMAZON_API_KEY', 'UK', 'AMAZON_SECRET_KEY');

$results = $client->itemSearch(array(
    'SearchIndex' => 'Books',
    'Power' => 'isbn:'.$isbn,
    'ResponseGroup' => 'Large,ItemAttributes,Images,EditorialReview'

$totalResults = $results->totalResults();
if ($totalResults == 1) {
    var_dump($results->current()); // causes PHP fatal

There are two ISBN numbers at the top of this script. The first causes the error, the second works fine. Both of these ISBNs are perfectly valid books that are listed on amazon.co.uk.

Unfortunately the problem is intermittent. I'm getting the error consistently at the moment, but the same search was working fine last week. I have had this problem in the past with other ISBNs though, so this isn't an isolated case specific to this book.

It does seem like this is partly a problem on Amazon's side, but it would be nice if ZF could detect the invalid result and handle it gracefully, as I have no way of catching the PHP fatal.


Attached are the raw XML responses I'm currently getting from Amazon for the two ISBNs in my example script. You'll see in the failing one that the XML indicates that there was 1 result matching the search, but no s are included.

Fixed the issue and merged it with 1.10 branch (r21883)