This is a small tutorial on how you can retrieve products from Amazon’s Associates Web Service. I’m not the biggest fan of Amazon’s affiliate program. The biggest reason is the one day cookie. I like programs that produce results, and I’ve always struggled with this program. When I talk to others that do have success, it definitely seems like you need a large volume of visitors, and it appears to be quite seasonally dependent.
Regardless, I wanted to show how you can make calls against their web service. The first step is to be a Amazon associate (https://affiliate-program.amazon.com/). Here you will get an associate tag (usually something-20). This allows you to receive commissions by creating widgets, etc. But we want to use the AWS (Amazon Web Service), so you must be a Amazon developer. So this step will involve us going to the Amazon Web Service web site (http://aws.amazon.com). After creating account you will need to create a token. Basically, this is a password used to login to Amazon’s web service. Look for a button on the upper right hand section called ‘Your Web Serices Account’, click on AWS Access Identifiers. Look for something down in the content called Access Key ID. You may need to generate them. To make the calls we’ll need the Access Key AND your Amazon associate tag.
Now.. to the code.
The first thing we need to understand is the search index. Basically, these are the categories at Amazon. This tells the web service what category we want to search for products. If you want all products from multiple categories, use ‘Blended’ as the search index. Here is a link that describes Amazon’s search index (http://docs.amazonwebservices.com/AWSECommerceService/2007-04-04/DG/SearchIndices.html).
Below is a listing of Search Indexes at Amazon:
Apparel
Automotive
Baby
Beauty
Blended
Books
Classical
DigitalMusic
DVD
Electronics
ForeignBooks
GourmetFood
HealthPersonalCare
Hobbies
HomeGarden
Jewelry
Kitchen
Magazines
Merchants
Miscellaneous
Music
MusicalInstruments
MusicTracks
OfficeProducts
OutdoorLiving
PCHardware
PetSupplies
Photo
Software
SoftwareVideoGames
SportingGoods
Tools
Toys
VHS
Video
VideoGames
Wireless
WirelessAccessories
Now we need to assemble a few variables for our web service call. They make it extremely easy using their REST method. We just need to assemble a querystring with our value. For this example, I want to search the ‘toys’ index and look for Star Wars items.
$searchIndex = ‘toys’;
$amazonAssoc = ‘something-20′;
$amazonId = ‘SOMELONGSTRING’;
$query = urlencode(‘star wars’);
$url = ‘http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&IdType=ASIN&ResponseGroup=Large&SearchIndex=" . $searchIndex . "&Operation=ItemSearch&Keywords=" . $query . "&AWSAccessKeyId=" . $amazonId . "&AssociateTag=" . $amazonAssoc . "&Version=2007-05-14′</code>
?>
After creating our querystring, we’re going to use cURL to send the request and retrieve an XML result back from the server.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $whaturl);
$data = curl_exec($ch);
curl_close($ch);
Now we have something back from cURL. We’ll use PHP5′s ximplexml_load_string() function to create a nice XML array. From here we just simply loop through the nodes and grab values we need.
$mark = $xml->TotalPages;
$error = $xml->ErrorMsg;
// check to see if we have items
if($xml->Items->Item){
$x = 0; // set counter to handle display amounts
foreach ($xml->Items->Item as $item){
if ($x < 5){
echo $item->ItemAttributes->Title."
";
echo "Link: http://www.amazon.com/exec/obidos/asin/" . $item->ASIN . "/ref=nosim/" .$amazontag."
";
}
}
}
unset($xml);
Below is a partial structure of the XML format. I’m sure I missed something, so you might want to crack open the XML files yourself to be sure. Hope this helps!!!
ASIN DetailPageURL SalesRank SmallImage URL Height Width MediumImage URL Height Width LargeImage URL Height Width ImageSets MerchantId ImageSet attributes Category SwatchImage URL Height Width SmallImage URL Height Width ThumbnailImage URL Height Width TinyImage URL Height Width MediumImage URL Height Width LargeImage URL Height Width ItemAttributes Feature IsAutographed ItemDimensions Label ListPrice Amount CurrencyCode FormattedPrice Manufacturer PackageDimensions PackageQuantity ProductGroup ProductTypeName Publisher SpecialFeatures Studio Title OfferSummary LowestNewPrice Amount CurrencyCode FormattedPrice LowestUsedPrice Amount CurrencyCode FormattedPrice TotalNew TotalUsed TotalCollectible TotalRefurbished Offers TotalOffers TotalOfferPages CustomerReviews AverageRating TotalReviews TotalReviewPages Review ASIN Rating HelpfulVotes CustomerId Reviewer CustomerId Name TotalVotes Date Summary Content EditorialReviews EditorialReview Source Content SimilarProducts SimilarProduct ASIN Title Accessories Accessory ASIN Title BrowseNodes BrowseNode BrowseNodeId Name Ancestors
Incoming search terms:
- amazon searchindex
- amazon catalog api
- amazon catalog web service
- catalog search amazon example delphi
- amazon searchindex blended
- amazon product catalog webservice java
- amazon catalog
- amazon api search in multible categories
- amazon catalogue api
- query amazon product catalog
Related posts:
- Finding BrowseNodes for Amazon API search (AWS)
- Simple script for connecting to Commission Junction’s Product Web Service and populating a local database
- Amazon Associates Web Service Changes
- CJ web service issues – unable to access WSDL (UPDATE)
- Amazon Product API: SignatureDoesNotMatch error response
Very nice code. Would be extremely easy to take it and turn it into a web application.