Amazon API: How To Get A Book’s Image

We are going to use Amazon’s API, and a PHP class I showed you in my last post, to get a book’s image.

This will be our setup.

<?php
// include the class
require_once 'Amazon.php';

// make a new object
$Amazon=new Amazon();

// set the search options
$parameters=array(
"region"=>"com",
"Operation"=>"ItemSearch", // we will be searching
"SearchIndex"=>"Books", // search in the books category
'ResponseGroup'=>'Images', // we want images
"Keywords"=>"0321185587" // this is what we are looking for, am using an ISBN
);

// get the signed url
$queryUrl=$Amazon->getSignedUrl($parameters);

// load the xml response into a variable
$response=simplexml_load_file($queryUrl);
?>

You can do many things other than search that is why we have to specify the operation of what we want to do.

The value I used for keywords is an ISBN, you don’t have to use ISBNs to search but I do because this gives me more accurate results. If instead of ISBNs you were to type the book’s title for example, Amazon would respond with more than one result, and although most of the time the first results is the most relevant there are times where it could be wrong.

Now that we have loaded the response into a variable we can use the values in it to show images. But before we use any of the values, we have to see if in fact there is a value, in other words we have to check if the number of results we got is greater than zero, and we do so like this.

if($response->Items->TotalResults > 0)
{
// we have at least one response

}
else
{
// no response

}

If our query returned at least one result we can proceed to get the URL for the book image depending on what size we need. Take a look at the response I got from my query, I have circled all the possible values we can use.
amazon-image-response

I would like to display the medium image, that would the the value in the second circle from the top down.
To get this value I can do the following.


$response->Items->Item->MediumImage->URL;

This gives me the URL, so to display the image I can put that URL as the src value of an image tag.

<img src="<?php echo $response->Items->Item->MediumImage->URL;?>" />

Instead of putting my image tag inside the “if” statement, am going to store the URL in a variable and use the tag at the end.

if($response->Items->TotalResults > 0)
{
   // we have at least one response
  $imagesrc=$response->Items->Item->MediumImage->URL;
  $alt='the book image';

}
else
{
   // no response, show some other image
  $imgesrc='no-image.png';
  $alt='no book image';
}
?>
<img src="<?php echo $imagesrc;?>"  alt="<?php echo $alt;?>"/>

And here is the full source.

<?php
$Amazon=new Amazon();

$parameters=array(
"region"=>"com",
"Operation"=>"ItemSearch", // we will be searching
"SearchIndex"=>"Books", // in the books category
'ResponseGroup'=>'Images',// we want images
"Keywords"=>"0321185587" // this is what we are looking for, you could use the book's title instead
);

$queryUrl=$Amazon->getSignedUrl($parameters);

$response=simplexml_load_file($queryUrl);

if($response->Items->TotalResults > 0)
{
   // we have at least one response
  $imagesrc=$response->Items->Item->MediumImage->URL;
  $alt='the book image';

}
else
{
   // no response, show some other image
  $imgesrc='no-image.png';
  $alt='no book image';
}
?>
<!-- show the image -->
<img src="<?php echo $imagesrc;?>"  alt="<?php echo $alt;?>" />

How are planning to use Amazon images? leave a comment 🙂