Hunting the Web Services at Amazon - Writing Your Application
(Page 2 of 4 )
What You Need To Do
The PHP NuSoap class provides a clean and simple way to access Amazon product information using the Amazon SOAP APIs . You will need to install the NuSoap library on your machine. This library can be downloaded from here . Download the file nusoap.php. You will also need free developers token; get it from here , all you need is your email address. Following picture depicts the development process.
Analyze Amazon Web Services
If you are new to Web Services, read introductory articles here . Now, I assume, that you understand the benefits of Web services, lets take a closer look to Amazon.com’s WSDL (Web Service Description Language).
This is nothing but a XML vocabulary and surprisingly simple, though it may look little bully at first look. With this document, we can view Web Service’s function, its location (end point) and special type. Click here to view the WSDL description for Amazon.com.
<xsd:complexType name="AuthorRequest">
<xsd:all>
<xsd:element name="author" type="xsd:string"
<xsd:element name="page" type="xsd:string" />
<xsd:element name="mode" type="xsd:string" />
<xsd:element name="tag" type="xsd:string" />
<xsd:element name="type" type="xsd:string" />
<xsd:element name="devtag" type="xsd:string"
<xsd:element name="sort" type="xsd:string" minOccurs="0" />
</xsd:all>
</xsd:complexType>
First, look at the <xsd:complexType> tag. This tag allows Amazon to create new types that provide additional functionality or grouping. In this case, we have an object that stores “Author search” criteria. Each type within this object is a string type. In the above code snippet, the elements wrap to the following keyword data:
Author – the author name used in this search
Page – Which page of the search result you would like to see
mode – A list of valid store
tag – “Webservice-20” this denotes, that this is a web service call
type – “lite” or “heavy”, this corresponds with the amount of data you want return
devtag – Your unit token (a free token is available at Amazon.com web site)
<message name="AuthorSearchRequest">
<part name="AuthorSearchRequest" type="typens:AuthorRequest" />
</message>
<message name="AuthorSearchResponse">
<part name="return" type="typens:ProductInfo" />
</message>
Another important element in the WSDL is the <message> tag; it provides information about the functions available from Web Services. In the above code snippet, the <part> element represents the parameters for the function named AuthorSearchRequest. You should also notice that we have two separate <message> elements, one named KeywordSearchRequest and the other AuthorSearchResponse. Request should be used when calling the function while response is used to provides the type returned from the function.
By combining our <message> and <xsd:complexType> elements, we can understand the function and the parameters it requires.
Write Your Application
Here is the full set of method calls with brief description that you can make remote call from your PHP application:
| Function (Methods) and parameters | Description |
| DoKeywordSearch ($Keyword, $Type = 'lite', $Category = 'books', $Max = DEFAULT_MAX) | Search for items (books, by default) matching the given keyword. |
| DoBrowseNodeSearch ($BrowseNode, $Type = 'lite', $Category = 'books', $Max = DEFAULT_MAX) | Search for the items (books, by default) within the given Browse Node. |
| DoAuthorSearch ($Author, $Type = 'lite', $Category = 'books', $Max = DEFAULT_MAX) | Search for items (books, by default) with the given author. |
| DoASINSearch ($ASIN, $Type = 'lite') | Search for items with the given ASIN or ASINs. Supply a PHP array as the first argument to search for multiple ASINs. |
| DoUPCSearch ($UPC, $Type = 'lite', $Category = 'music') | Search for item (music, by default) with the given UPC. |
| DoManufacturerSearch ($Manufacturer, $Type = 'lite', $Category = 'books', $Max = DEFAULT_MAX) | Search for the items (books, by default) with the given manufacturer. |
| DoActorSearch ($Actor, $Type = 'lite', $Category = 'dvd', $Max = DEFAULT_MAX) | Search for the items (books, by default) with the given manufacturer. |
Table 1: Amazon’s Web Services function list
All parameters except the first are optional. $Type can be 'lite' or 'heavy'. $Category must be a valid Amazon category. $Max is the maximum number of results to return (10 is the default).
The skeleton of our client code (pseudo codes) will look something like following:
<?php
If (!$_POST[‘query’])
{
//display form
} else {
//execute query at Amazon
}
As you can see, the script is split into two sections, one for the search form and the other for the search result. An "if" statement is used to decide which section of the script to execute.
Next: Walkthrough Example >>
More HTML Articles
More By Ahm Asaduzzaman