http://www.developer.com/

Back to article

Amazon Web Services and the Zend Framework


November 24, 2008

I recently completed work on a newly launched website named GameNomad, a service that makes it easy for friends to monitor and share their game collections. The site uses Amazon's Web services infrastructure to plug into Amazon.com's enormous product database, retrieving a variety of video game-related information such as the title, release date, price, platform, and game cover. Used in conjunction with Amazon's affiliate program, developers short on resources but flush with imagination can create a host of fascinating, useful, and lucrative online services.

In this tutorial, I'll show you how to use the Zend Framework's Zend_Service_Amazon component to retrieve product data from Amazon.com's product database. Although the focus of the tutorial will be video games, you can easily adapt what you learn here to any of the dozens of other product categories, whether it;s gourmet food, tools, toys, or books!

Creating an Amazon Web Services Account

Before you can begin taking advantage of Amazon's Web Services program, you'll need to sign up for an account. Doing so is free, and if you already have an Amazon customer account, you're practically done. Just head over to the Amazon Web Services homepage and click the "Sign Up Now" button. You'll be asked to log in to your existing Amazon.com customer account, or create a new account.

Creating this account results in the generation of an Access Key ID; Amazon uses this key to identify you when querying their Web service. In the next section, you'll learn how to pass this key through the Zend Framework's Zend_Service_Amazon component; therefore, taking a moment to copy it down for later reference might be a good idea. To access this key, click on the "Your Account" tab found at the top of the Amazon Web Services homepage, and on the ensuing page, click the "Access Identifiers" link. Your Access Key ID will be presented on this page.

Connecting to Amazon with Zend_Service_Amazon

Connecting to Amazon's Web Service is a trivial process using Zend_Service_Amazon. Just invoke the Zend_Service_Amazon class, passing along your Access Key ID. I store my Access Key in the application's config.ini file for easy management:

$amazon = new Zend_Service_Amazon($this->config->
   webservices->amazon_key);

Once connected, you're ready to begin querying the database. You'll spend much of the remainder of this tutorial doing exactly that.

Searching for a Video Game by ASIN

Amazon.com has long relied upon its own product identification system known as ASIN (Amazon Standard Identification Number). ASINs are character-based codes that uniquely identify every product sold via the website. For instance, Amazon's ASIN for the game "Super Mario Galaxy" (Nintendo Wii) is B000FQ9QVI. To retrieve the data tied to this game, you can query Amazon by using this ASIN:

$amazon = new Zend_Service_Amazon($this->config->
   webservices->amazon_key);
$game = $amazon->itemLookup('B000FQ9QVI');

This will return an object of the type Zend_Service_Amazon_Item that contains product data tied to that ASIN. However, by default only data found within a response type known as "Small" will be available; this contains only essential information such as the product title, ASIN, and manufacturer. You can, however, tell Amazon to return successively larger amounts of information by setting the response type to "Medium" or "Large." In particular, if you set it to "Large" you'll also receive links to the product image; these generally can be useful. To change the response type, pass it along as an array element to the itemLookup() method:

$amazon = new Zend_Service_Amazon($this->config->
   webservices->amazon_key);
$game = $amazon->itemLookup('B000FQ9QVI',
   array('ResponseGroup' => 'Large'));

From here, you can begin retrieving the attributes available to the $game object. For instance, to retrieve the title and price, you'd access the Title and FormattedPrice attributes, respectively:

echo "{$game->Title} costs ${$game->FormattedPrice}";

Returning:

Super Mario Galaxy costs $49.99

Searching for Video Games by Title

Of course, it's relatively unlikely a user will know the product's ASIN, so title-related searches are much more commonplace. To search by title, you'll use the ItemSearch() method:

$amazon = new Zend_Service_Amazon($this->config->
   webservices->amazon_key);
$games = $amazon->itemSearch(array('SearchIndex' =>
   'VideoGames', 'Title' => 'Mario', 'ResponseGroup' => 'Large'));

From here, you can parse the result set, retrieving the desired attributes. For instance, to retrieve a list of all titles found in Amazon's database containing the term "Mario", use this code:

foreach($games AS $game) {
   echo "{$game->Title}<br />";
}

This returns the following information:

Mario Kart Wii with Wii Wheel
Mario Kart DS
Super Mario Galaxy
New Super Mario Bros.
Mario & Sonic at the Olympic Games
Mario Party DS
Mario Super Sluggers
Mario Party 8
Mario & Sonic at the Olympic Games
Super Paper Mario

This list underscores a very important point to keep in mind when using Amazon's Web Service. Video game aficionados will quickly realize this isn't a complete list of all known games containing the term "Mario" in the title. This is because Amazon's database isn't a video game archive!! Rather, it's simply a catalog of all products currently sold on Amazon.com. Therefore, you cannot rely upon it to provide you with every product ever made, and instead will need to create additional tools for manually adding product information to your own database in the case it's not found on Amazon.com.

Displaying Product Images

Chances are you'll want to display a product image alongside any other relevant product image. Amazon provides you with access to these images by way of a URL. To retrieve the image, set the response grouping to "Large" when searching, and subsequently refer to the SmallImage->Url, MediumImage->Url, and LargeImage->Url attributes, respectively. Because these values point directly to the image, you'll need to embed them within an <img> tag:

<img src='{$game->MediumImage->Url}' />

On GameNomad, I use a similar approach to render all of the video game covers, as shown in Figure 1:



Click here for a larger image.

Figure 1: Retrieving video game covers using Amazon Web Services

Using the Alternative Query API

The Zend_Service_Amazon component also supports a second method for querying Amazon's Web service, known as the Query API. This method streamlines the code otherwise required to interact with the Web service. An example follows:

$query = new Zend_Service_Amazon_Query($this->config->
   webservices->amazon_key);
$query->category('VideoGames')->Title('Mario')->
   ResponseGroup('Large');
$games = $query->search();
foreach ($games AS $game) {
   echo "{$game->Title}<br />";
}

This code produces a result identical to that used to perform the earlier title search. Because the ultimate outcome is identical no matter the approach, the method you choose is merely a matter of preference. Personally, I prefer the alternative query API because it seems to produce more legible code, but the choice is ultimately yours.

Where to From Here?

Sending email from your PHP-powered websites is easy once you've been provided with the necessary background, so hopefully this tutorial helped alleviate any initial confusion you had in this regards. For further information, check out the following resources for more information about sending email using PHP:

About the Author

Jason Gilmore is founder of a Web development and consulting firm based out of Columbus, Ohio. Formerly Apress' open source editor, Jason fostered the development of more than 60 books, along the way helping to transform their open source line into one of the industry's most respected publishing programs. He's the author of several books, including the best-selling Beginning PHP and MySQL: From Novice to Professional (currently in its third edition), Beginning PHP and PostgreSQL: From Novice to Professional, and Beginning PHP and Oracle: From Novice to Professional.

Jason is cofounder of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer's conference, and was a member of the 2008 MySQL Conference speaker selection board. Jason has over 100 articles to his credit within prominent publications such as Developer.com, Linux Magazine, and TechTarget.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date