link to netboffin homepage

 

Amazon ItemSearch In PHP

ItemSearch with SOAP:

  If you go to Amazon's website you can search for items by typing in keywords into a text box and clicking on the search button. Results are then displayed on a series of pages. Amazon's web service allows you to perform the same operation without going to their website. Instead you can write a computer program that sends queries to the Amazon web service in the form of a SOAP request and retrieves the results in the form of an XML SOAP response. The program can then extract relevant data from the XML, process it and display it for the user.

SOAP stands for Simple Object Access Protocol and is a type of XML document which can be used to call functions on a web service. In order to do so the XML has to contain details of the particular method being called and the parameters to be passed to it. How do we know what methods we can call on a web service? We use a WSDL file provided by the web service owner.

WSDL stands for Web Service Description Language and is a type of XML document which describes:

Using the Amazon ItemSearch Operation using a SOAP request with PHP isn't too complicated once you understand the basic process and data types.

  1. Get the WSDL file for the web service
  2. Create an instance of the SOAPClient Class passing the constructor the address of the WSDL file
  3. Use the Methods of the SOAPClient which are defined in the WSDL eg ItemSearch and pass it the parameters also defined in the WSDL

PHP 5 allows us to create a SOAPClient object from a WSDL file

Here’s how:

$wsdl_url = 'http://webservices.amazon.com/'.
'AWSECommerceService/AWSECommerceService.wsdl?';

$client = new SoapClient($wsdl_url);

The new instance of SoapClient has all the methods defined in the WSDL file including the ItemSearch method.

Now all we need to know is which parameters we need to pass the ItemSearch method of our new SOAPClient Object and how to retrieve the fields we want to display from the returned result.

Once we've created a SOAP client from the WSDL file we pass it's ItemSearch method the necessary parameters defined in the WSDL file.

Looking in more detail at the parameters we pass the ItemSearch method, it consists of the following required items.

  1. An AWSAcessKeyId which you get when you get an Amazon Associates Account
  2. An Operation type for example ItemSearch or ItemLookup
  3. A Request which in turn consists of :
    1. A SearchIndex
    2. Keywords
    3. A ResponseGroup which in turn consists of:
      1. Small
      2. Medium
      3. Large
      4. ItemAttributes
      5. Images
      6. SalesRank
      7. Reviews
      8. EditorialReview
      9. Similarities
      10. ListmaniaLists
      11. And others

Here's a link to Amazon's detailed explanation of the parameters to be passed to the ItemSearch Operation.

How do we build the PHP method call? The parameters can be created in one of two ways:

  1. Using Object Syntax
  2. Using an Associative Array

To use object syntax:

$params->AWSAccessKeyId = 'YOUR AMAZON APPID';
$params->Operation = 'ItemSearch';
$params->Request->SearchIndex = 'Books';
$params->Request->Keywords = 'php5 Objects';

To use an associative array:

First we create an associative array with the names of the parameters as the keys. The values are dependent on what the parameter is.

The simplest ItemSearch parameters we can send are at the highest Level:

$params = array(

'AWSAccessKeyId'=>'YourAPPID',

'Operation'=>'ItemSearch',

'Request'=>$searchrequest

);

 

The Request parameter holds an array we've called $searchrequest  which is defined thus:

$searchrequest=array(

'SearchIndex'=>'Books',

'Keywords'=>'Harry Potter'

);

The SearchIndex parameter is required for the itemSearch

The ResponseGroup parameter of the Request can take many values.How do we deal with this? Instead of putting all the values in an array we use a comma separated list like this.

$searchrequest=array(

'SearchIndex'=>'Books',

'Keywords'=>'Harry Potter'

'ResponseGroup'=>'Medium,Images,Reviews'
);

When the itemSearch method of our SOAPClient runs it will return our results with the appropriate fields as defined by the ResponseGroup. If there's no ResponseGroup parameter in the request, then by default the method returns a Small set of results. Here's how Amazon's site describes the Small response group.

The Small response group provides global, item-level data (no pricing or availability), including the ASIN, product title, creator (author, artist, composer, directory, manufacturer, etc.), product group, URL, and manufacturer. Small includes all of the data necessary to display a typical item in a search results listing.

OK so now we have retrieved the response from the server, how do we access the data and display it on the page?

The response is in the form of XML but is converted by the SOAPClient into an Object which we can access through object syntax. We want to look in detail at the class variables in this object and how we can access them, but to start with here's a simple example.

$books = $client->ItemSearch($params);
$loop=0;
foreach($books->Items->Item as $item)
{
echo $books->OperationRequest->HTTPHeaders->Header->Name;
echo $book->Items->Item->ASIN;
echo $books->Items->Item->ItemAttributes->Title;

echo "<div class='ItemHolder'>Item ".$loop++;
echo $item->ItemAttributes->Title;
echo "</div>";
}
blog comments powered by Disqus