August 29, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Amazon Web Services and the Zend Framework

  • November 24, 2008
  • By Jason Gilmore
  • Send Email »
  • More Articles »

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

Tags: Web services



Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel