You need to use the full package name and manually install XML_Serializer, rather than simply "pear install Services_Amazon," because they are still considered beta releases. Keep this in mind; if you update these packages later, the interface may change. In my experience, while they are "beta," they still provide a robust and useful encapsulation of the Amazon API.
To use the Amazon API, you'll need to obtain an Access Key and Associate ID. For the Access Key, you'll need to sign up for an Amazon Web Services Account, and then navigate to the "View Access Key Identifiers" section, where you can cut and paste the access key. To obtain an Associate ID, you'll need to sign up for the Amazon Affiliate Program, and receive an Associate ID during that process. This ID allows Amazon to credit your account with a percentage of sales generated through your use of their web services.
The Services_Amazon package includes an interface to the disabled Amazon ECS3 service, in addition to the active ECS4 service. You'll want to be sure to use the latter. The code below assumes that you've loaded your access key and associate ID into variables named $ACCESS_KEYY and $ASSOC_ID.
At this point, we've created an interface object through which we'll perform our interactions. The Amazon API uses a simple REST (Representational State Transfer) API, so a connection isn't established until you actually issue requests. At that point, the API will make an HTTP GET request to the appropriate Amazon URL and return a pre-parsed dataset. Most API calls will return an associative array including the Result Code, Request Parameters and Result data.
To get an idea for what the API is doing behind the scenes, you can use the getLastuUrl() and getRawResult() methods on the Services_AmazonECS4 instance.
$results = $amazon->itemSearch('Books',
array('Keywords' => 'web development'));
if (PEAR::isError($results)) {
die('Sorry, there was a problem with your search: ' .
Don't Make Me Think: A Common Sense Approach to Web Usability, 2nd Edition
</Title>
</ItemAttributes>
</Item>
... More Items ...
</Items>
</ItemSearchResponse>
As you can see, the Services_Amazon interface takes quite a bit of complexity and turns it into some simple API calls. Here we'll again make use of the itemSearch() method and generate a listing of books.
The API takes care of many details: parsing the XML, turning the Authors into an array and checking for errors.
In addition to searching the Amazon inventory, the API also allows for the creation and maintenance of a shopping cart. You can add items to the cart based on the ASIN parameter of the product results. Once the user is done shopping you'll redirect them or provide them a link to the cart's PurchaseURL, where Amazon will take care of billing the user and crediting your Associate ID with a portion of the sale.
In this example, we create a cart, add two pre-selected items to it, and send the user to Amazon.
$cart = $amazon->CartCreate(
array('ASIN' => '0321344758', 'Quantity' => 1));
$amazon->CartAdd($cart['CartId'], $cart['HMAC'],
array('ASIN' => '0596517742', 'Quantity' => 2));
header('location: ' . $cart['PurchaseURL']);
For brevity, error checking of the API calls has been omitted. For a production application, it should always be included.
The Amazon API with the Services_Amazon wrapper from PEAR provides an easy way to allow users to search for books, music and nearly everything else under the sun. When used with an associate id, it is a simple way to build a store front using the Amazon System.