PHP DevCenter
oreilly.comSafari Books Online.Conferences.


A PHP Web Services Client
Pages: 1, 2

Parameters like manufacturer and mode control what you're searching for — in this case — books manufactured by O'Reilly. The sort, page, and type options specify what data is to be returned.

Results can optionally be ordered from best- to worst-selling, alphabetically, by price, and more. (The + in front of salesrank means ordered from best-selling; -salesrank reverses the order.) To conserve resources, Amazon only returns ten items per request. Setting page to 1 means return books 1 through 10; page 2 has books 11 through 20; and so forth. If you want more than ten items on your page, just create a loop and increment page each time through. Also, since Amazon has so much information, they've created two different DTDs that specify what information they'll return: lite and heavy. Here, you get the lite results, which have the product name, author, price, three different image URLs, and the manufacturer. The heavy DTD has everything, including all of the posted reviews, any ListMania lists containing the work, and links to music clips for CDs.

Also, you pass in a tag and a devtag. The tag is an Amazon associate name, so you can collect referral fees. The devtag is the developer's token you received at the beginning of the article.

You don't need to worry about the parameter order. That's all taken care of for you behind the scenes. That's not all that's done behind closed doors, however. When you finally make the request, by calling $client->ManufacturerSearchRequest($params), PEAR:SOAP converts your PHP data structures to a SOAP message written in XML and sends an HTTP request to Amazon's server. After Amazon receives and processes your query, it replies with a SOAP message of its own. PEAR::SOAP listens for this response and parses the XML into a PHP object, which is then returned by our method and stored in $books.

When you're unsure what's in a variable, the easiest way to inspect it is to use print_r(). Calling print_r($books) echoes:

stdClass Object
  [TotalResults] => 822
  [TotalPages] => 83
  [Details] => Array
      [0] => stdClass Object
          [Url] =>
          [Asin] => 0596004478
          [ProductName] => Google Hacks
          [Catalog] => Book
          [Authors] => Array
              [0] => Tara Calishain
              [1] => Rael Dornfest

          [ReleaseDate] => 01 February, 2003
          [Manufacturer] => O'Reilly & Associates
          [ImageUrlSmall] =>
          [ImageUrlMedium] =>
          [ImageUrlLarge] =>
          [ListPrice] => $24.95
          [OurPrice] => $17.47
          [UsedPrice] => $15.95

      [insert nine additional books here]



The object has three properties: TotalResults, the number of items that the search matches; TotalPages, the number of times you need to increment $page and requery Amazon to gather all the data; and Details, an array containing the actual catalog information. Each element of Details is itself another array, with the properties you can use to do things like making your own Amazon store or printing information about the pile of books next to your bed. (I've shortened some of the data so it fits nicely in the screen.)

For example, here's a quick-and-dirty loop to create HTML showing the book title, author(s), and price next to a JPEG picture of the cover:

foreach ($hits->Details as $hit) {
    $ProductName = html_entities($hit->ProductName);
    $Authors     = join(' and ', $hit->Authors);

    print <<< _HTML_
    <div style="clear:left; width: 300px; padding:5px; 
                margin:5px; background:#ddd;">
    <a href="$hit->Url"><image src="$hit->ImageUrlSmall" 
                alt="$ProductName" align="left"></a>
    By $Authors<br/> Price: $hit->OurPrice<br/>

This iterates through $hits::Details and places each book inside of its own <div>. (Of course, a cleaner solution is to place the style information in a class instead of embedding it inside of each <div>.) Within the loop, you can pull out the data you want to show. If it's already formatted how you like, print it directly. Or, if it needs a little massaging, like the authors array, reformat it, as is done above by calling join(). You also need to call html_entities() on ProductName and other text fields, because you need to encode characters like the & in O'Reilly & Associates.

You can see the results in Figure 1.

reformatted book data
Figure 1. Reformatted book data from the Amazon database

Amazon also allows more complex queries that filter the listings on multiple criteria. You can use PowerSearchRequest() with Boolean logic to construct a search. For example, to restrict your O'Reilly search to only books on PHP:

$params = array(
    'power'  => "publisher:O'Reilly AND keywords:PHP",
    'mode'   => 'books',
    'sort'   => '+title',
    'page'   => 1,
    'type'   => 'lite',
    'tag'    => 'trachtenberg-20',
    'devtag' => 'XXXXXXXXXXXXXX',

$hits  = $client->PowerSearchRequest($params);

Only the first array element has changed. It's now power instead of manufacturer. Also, its value, publisher:O'Reilly AND keywords:PHP, makes sure that the publisher is O'Reilly AND that the book is on PHP. You can also use OR and NOT as well as group expressions using (parenthesis).

Now, the same display function generates new output:

a filtered search
Figure 2. A filtered search


As you've seen, it's very simple to use SOAP and WSDL with PHP. These clients allow you to gather information from across the net to use in your scripts. is not the only major company to provide a SOAP front end to its data. Google lets you search their listings up to 1,000 times a day. Additionally, XMethods has a large directory of SOAP servers that you can experiment with and use.

Adam Trachtenberg is the manager of technical evangelism for eBay and is the author of two O'Reilly books, "Upgrading to PHP 5" and "PHP Cookbook." In February he will be speaking at Web Services Edge 2005 on "Developing E-Commerce Applications with Web Services" and at the O'Reilly booth at LinuxWorld on "Writing eBay Web Services Applications with PHP 5."

Return to the PHP DevCenter.

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: