Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Using Amazon E-Commerce Services With PEAR SOAP
Integrate Amazon.com's ECS service into your own Web site.

| Searching For Godot |

In the real world, it's quite likely that customers won't know the ASIN for the product they're looking for. That's why ECS also includes the ability to search the Amazon.com catalog, via its ItemSearch() method call.

You can search against most product attributes, including keyword, author, title, artist, manufacturer, brand, composer, condition, price and many more. Obviously, the attributes against which the search can be performed differ from product category to product category, and you're expected to apply some common sense when performing a search - it wouldn't be logical to search the Electronics category by Author, for example. The ECS developer guide contains detailed information on which attributes are available in each product category.

Here's an example that illustrates the ItemSearch() method:


<html>
<head><basefont face="Arial"></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search Amazon.com Books by
<select name="c">
    <option>Author</option>
    <option>Title</option>
    <option>Keywords</option>
</select>
for
<input type="text" name="q">
<input type="submit" name="submit">
</form>
<?php
if ($_POST['q']) {
    // include SOAP class
    include("SOAP/Client.php");
   
    // initialize SOAP client
    $soapclient = new SOAP_Client("http://webservices.amazon.com/onca/soap?Service=AWSECommerceService");
   
    // set data for SOAP request
    $params = array('SubscriptionId' => 'YOUR-ID-HERE',
                    'SearchIndex' => 'Books',
                    $_POST['c'] => $_POST['q']);         
   
                   
    // get item information
    $result = $soapclient->call("ItemSearch", $params);
 
    if (PEAR::isError($result)) {
        die("Something went wrong...");
    }

    // get number of results
    $numResults = $result['Items']->TotalResults;

    // format and display results
    $data = $result['Items']->Item;
    echo "<h2>Search Results</h2>";
    echo "$numResults result(s) found <p />";
    // if more than one item
    // iterate over item array
    if (is_array($data)) {
        foreach ($data as $item) {
            showProductInfo($item);
        }
    // if only one item
    } else {
        showProductInfo($data);
    }
}

// function to display product information
function showProductInfo($item) {
    if (isset($item->ItemAttributes->Title) && isset($item->ItemAttributes->Author)) {
        echo "<b><a href=" . $item->DetailPageURL . ">" . $item->ItemAttributes->Title . "</b></a> <br />";
        echo $item->ItemAttributes->Author . " <p />";
    } else {
        echo "<a href=" . $item->DetailPageURL . ">[ASIN: " . $item->ASIN . "]</a>  <br />Cannot find title/author information for this product <p />";
    }
}
?>
</body>
</html>


Here's what it looks like:

Output image

This listing contains a query form for Amazon.com's Books section. It allows you to search the book catalog by title, author or keyword, and contains an input field for one or more query terms. Once submitted, a SOAP request for the ItemSearch() method is created and transmitted to the SOAP server.

There are two arguments that must be passed to the ItemSearch() method. The first argument, "SearchIndex",  specifies the Amazon.com store to search (in this case, "Books", although you can also use "Apparel", "Baby", "Music", "HomeGarden", "Jewelry" and others - consult the ECS documentation for a complete list), while the second specifies the attribute to search against (in this case, "Title", "Author" or "Keywords", although again you can get a complete list from the ECS developer guide) with the user's search term.

The SOAP response to an ItemSearch() request usually contains the total number of matches found, the number of pages (more on this shortly) and details on each matching item. It's fairly easy to process this data into a search results page, complete with links to the corresponding product detail page on Amazon.com. Remember that you can attach a "ResponseGroup" argument to the request as well, to control the amount of information retrieved for each product.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback