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.

| Cracking The Code |

I'll begin with a simple example that demonstrates the power of ECS - looking up a book on Amazon.com. Here's the code:


<?php
// 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',
                'ItemId' => '0385504209');         

// call an ECS method
$result = $soapclient->call("ItemLookup", $params);

// check for errors
if (PEAR::isError($result)) {
    die("Something went wrong...");
}
// else print ECS return values
print_r($result);
?>


The first order of business is to include the SOAP class file, which contains all the methods needed to access SOAP services, and create an instance of the client. The client is initialized with the URL to Amazon's ECS service (in case you're wondering where I got this URL from, it's listed in the ECS developer kit). Once the client is initialized, its call() method is used to create and transmit a SOAP request to the ECS server; the server's response is then printed to the output device.

Every item in the Amazon.com product catalog is tagged with a unique identifier. This identifier is called an ASIN, or Amazon Stock Identification Number. To look up a specific item in the catalog, ECS offers the ItemLookup() method, which accepts one or more ASINs as input and returns product information for each.

In this script, I've used this ItemLookup() method with ASIN 0385504209, which happens to refer to Dan Brown's "The Da Vinci Code". Here's the response from Amazon.com:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Array
(
    [OperationRequest] => stdClass Object
        (
            [HTTPHeaders] => stdClass Object
                (
                    [Header] =>
                )

            [RequestId] => 1J06J96NXDPE2HVB596S
            [Arguments] => stdClass Object
                (
                    [Argument] =>
                )

            [RequestProcessingTime] => 0.0101399421691895
        )

    [Items] => stdClass Object
        (
            [Request] => stdClass Object
                (
                    [IsValid] => True
                )

            [Item] => stdClass Object
                (
                    [ASIN] => 0385504209
                    [DetailPageURL] => http://www.amazon.com/exec/obidos/redirect?tag=ws%26link_code=sp1%26camp=2025%26creative=165953%26path=http://www.amazon.com/gp/redirect.html%253fASIN=0385504209%2526tag=ws%2526lcode=sp1%2526cID=2025%2526ccmID=165953%2526location=/o/ASIN/0385504209%25253FSubscriptionId=YOUR-ID-HERE
                    [ItemAttributes] => stdClass Object
                        (
                            [Author] => Dan Brown
                            [ProductGroup] => Book
                            [Title] => The Da Vinci Code
                        )

                )

        )

)
').'
'

Is that appealing, or just appalling? You decide...

A close look at the output above will reveal that no, it's not a bad hair day, but an array of nested objects containing information on the product category (books), together with the book's title, author, ASIN and Amazon.com page URL. It's not particularly readable at the moment, so let's pretty it up a bit before proceeding with the explanation:


<html>
<head><basefont face="Arial"></head>
<body>
<?php
// 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',
                'ItemId' => '0385504209');         

// get item information
$result = $soapclient->call("ItemLookup", $params);

// check for errors
if (PEAR::isError($result)) {
    die("Something went wrong...");
}

// else print ECS return values
$data = $result['Items']->Item;   
?>
<h2><?php echo $data->ItemAttributes->Title; ?></h2>
<?php echo $data->ItemAttributes->Author; ?>
<p />
<a href="<?php echo $data->DetailPageURL; ?>">Amazon.com Product Page...</a>
</body>
</html>


Here's what it looks like now:

Output image

The sharper-eyed amongst you will have noticed that the call to ItemLookup() in the listings above actually contains two parameters. The "SubscriptionId" parameter contains your ECS developer token, while the "ItemId" parameter contains the ASIN to search for. Different ECS method calls require different parameters - look up the developer guide to see a list - but the "SubscriptionId" parameter is mandatory for all of them.

In case you're wondering how I got the ASIN in the first place, it's pretty simple: just look in the URL of the corresponding product page on Amazon.com. For example, if you browse to Amazon.com's product page for "The Da Vinci Code", you'll see that the URL looks like this:


http://www.amazon.com/exec/obidos/ASIN/0385504209/


The product ASIN is the identifier following the ASIN string in the URL. You can use this method to locate ASINs for any product on Amazon.com. Note, however, that the values returned by ItemLookup() differ from product category to product category; a book lookup operation will return author and title information, while a DVD lookup operation will return cast, director and genre information. You can look in the ECS developer kit to understand the expected return values for each Amazon.com product category.


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