Using AWS for Amazon Search
Amazon's
Product Advertising API? (formerly ECS) provides access to the information you can find on the website. Integrating it in your own application is easy, thanks to libraries that wrap the interface and do all the heavy lifting for you. I've had a look at creating an Amazon search engine with
CloudFusion?.
First steps
Setup is just about as simple as it gets. Download the latest release, unpack it into a directory on your webserver, edit the short configuration file and rename it to
config.inc.php. Now we just need a simple file to test our setup.
<?php
require_once 'cloudfusion/cloudfusion.class.php';
$pas =
new AmazonPAS
();
var_dump($pas->
item_search("amazon"));
?>
And that's it! The result is nothing you could put on your webpage, but at least we know that it works now.
Displaying the data
While the documentation for the
CloudFusion AmazonPAS? class is very good and the documentation of the
ResponseGroup part of the Amazon API? is very extensive as well, it can be somewhat difficult to figure out where the things that you want are. I found it easiest to simply look at the XML of the response --
var_dump is your friend.
So, first to display the titles that the search gaves us back.
<?php
require_once 'cloudfusion/cloudfusion.class.php';
$pas =
new AmazonPAS
();
$res =
$pas->
item_search("amazon");
if($res->
isOK()) {
foreach($res->
body->
Items->
Item as $item) {
echo $item->
ItemAttributes->
Title .
"<br/>";
}
}
?>
Nine West Central Time Medium E/W Satchel,Black/Black/Black,one size
The Lost City of Z: A Tale of Deadly Obsession in the Amazon (Vintage Departures)
Etienne Aigner Tucson Tote,Patchwork,one size
Sell on Amazon: A Guide to Amazon's Marketplace, Seller Central, and Fulfillment by Amazon Programs
Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)
Bearpaw Women's M410 10" Boot,Black,9 M US
KATHY Van Zeeland Drop Off Service A Line Shopper,Frosting,one size
Amazon Kindle Black Leather Cover w/ strap (Fits 6" Display, Latest Generation Kindle)
Amazon Top Seller Secrets: Insider Tips from Amazon's Most Successful Sellers
Etienne Aigner Tuscon Bucket Hobo,Garden Floral,one size
That's better. We can dive a bit deeper and get some more information. Let's change the
ResponseGroup to
Large as well to get additional information back from the service.
<?php
require_once 'cloudfusion/cloudfusion.class.php';
$pas =
new AmazonPAS
();
$res =
$pas->
item_search("amazon",
array('ResponseGroup' =>
'Large'));
if($res->
isOK()) {
echo "<table>";
foreach($res->
body->
Items->
Item as $item) {
echo "<tr>";
echo "<td>" .
$item->
ItemAttributes->
Author .
"</td>";
echo "<td>" .
$item->
ItemAttributes->
Title .
"</td>";
echo "<td>" .
$item->
CustomerReviews->
AverageRating .
"</td>";
echo "<td>" .
$item->
Offers->
Offer->
OfferListing->
Price->
FormattedPrice .
"</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Nine West Central Time Medium E/W Satchel,Black/Black/Black,one size $89.00
David Grann The Lost City of Z: A Tale of Deadly Obsession in the Amazon (Vintage Departures) 4.0 $9.32
Etienne Aigner Tucson Tote,Patchwork,one size $79.99
Steve Weber Sell on Amazon: A Guide to Amazon's Marketplace, Seller Central, and Fulfillment by Amazon Programs 4.5 $17.05
Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation) 4.0 $259.00
Bearpaw Women's M410 10" Boot,Black,9 M US 4.0 $54.03
KATHY Van Zeeland Drop Off Service A Line Shopper,Frosting,one size $99.00
Amazon Kindle Black Leather Cover w/ strap (Fits 6" Display, Latest Generation Kindle) 4.0 $34.99
Brad Schepp Amazon Top Seller Secrets: Insider Tips from Amazon's Most Successful Sellers 4.0 $16.47
Etienne Aigner Tuscon Bucket Hobo,Garden Floral,one size $64.99
It looks like we're not getting the same information back for all of the items. Unfortunately, there's nothing we can do about that. Have a look at the response from the service to see if there's another attribute that you could use if one is unavailable. For example, apart from
Author, there's also
Creator and
Manufacturer.
Getting more data
The service only returns results for the first 10 items. We can get more by setting
ItemPage in the request.
<?php
require_once 'cloudfusion/cloudfusion.class.php';
$pas =
new AmazonPAS
();
$res =
$pas->
item_search("amazon",
array('ResponseGroup' =>
'Large',
'ItemPage' =>
2));
if($res->
isOK()) {
echo $res->
body->
Items->
TotalResults .
" results on " .
$res->
body->
Items->
TotalPages .
" pages of results<br/>";
echo "<table>";
foreach($res->
body->
Items->
Item as $item) {
echo "<tr>";
echo "<td>" .
$item->
ItemAttributes->
Author .
"</td>";
echo "<td>" .
$item->
ItemAttributes->
Title .
"</td>";
echo "<td>" .
$item->
CustomerReviews->
AverageRating .
"</td>";
echo "<td>" .
$item->
Offers->
Offer->
OfferListing->
Price->
FormattedPrice .
"</td>";
echo "</tr>";
}
echo "</table>";
}
?>
20802817 results on 2080282 pages of results Etienne Aigner Tuscon Bucket Hobo,Garden Floral,one size $64.99
Etienne Aigner Tucson Tote,Garden Floral,one size $69.99
Robert Spector Amazon.com: Get Big Fast 3.5 $12.48
Seventh Generation Chlorine Free Baby Diapers, Stage 2 (12-18 Lbs.), Case of 160 Diapers [Amazon Frustration-Free Packaging] 4.0 $39.99
Nine West Central Time Small Cross Body,Black/Black/Black,one size $52.63
crocs Women's Malindi Flat Slingback,Black,7 M 4.5 $28.02
Rebecca Saunders Big Shots, Business the Amazon.com Way: Secrets of the Worlds Most Astonishing Web Business (2nd Edition) 2.5 $19.46
Steve Madden Men's S089 Aviator Sunglasses,Gun-Blue Frame/Gradient Smoke Lens,one size $40.00
Amazon Kindle DX Leather Cover (Fits 9.7" Display, Latest Generation Kindle DX) 3.5 $49.99
Roger Harris The Amazon, 3rd: The Bradt Travel Guide 4.5 $17.15
Linking back to Amazon
Just a list of results is not terribly useful. Let's link back to the Amazon detail pages.
<?php
require_once 'cloudfusion/cloudfusion.class.php';
$pas =
new AmazonPAS
();
$res =
$pas->
item_search("amazon",
array('ResponseGroup' =>
'Large'));
if($res->
isOK()) {
echo "<table>";
foreach($res->
body->
Items->
Item as $item) {
echo "<tr>";
echo "<td>" .
$item->
ItemAttributes->
Author .
"</td>";
echo "<td><a href=\"" .
$item->
DetailPageURL .
"\">" .
$item->
ItemAttributes->
Title .
"</a></td>";
echo "<td>" .
$item->
CustomerReviews->
AverageRating .
"</td>";
echo "<td>" .
$item->
Offers->
Offer->
OfferListing->
Price->
FormattedPrice .
"</td>";
echo "</tr>";
}
echo "</table>";
}
?>
This also works for other Amazon sites, just change the locale.
$pas->set_locale("uk");
Bruce Parry Amazon. An Extraordinary Journey Down The Greatest River On Earth 5.0 £14.49
Vax V-124A Dual V Upright Carpet Washer 4.5 £249.99
AmazonBasics 3M Quad-Shielded High Speed HDMI Cable [Amazon Frustration-Free Packaging] 4.5 £12.99
Amazon [DVD] [2008] 4.5 £8.47
Brad Schepp Amazon Top Seller Secrets: Inside Tips From Amazon's Most Successful Sellers 5.0 £11.21
Dyson DC14 Origin Upright Vacuum Cleaner for Carpeted Homes 5.0 £178.70
AmazonBasics 2M Dual-Shielded High Speed HDMI Cable [Amazon Frustration-Free Packaging] 4.5 £4.99
Medical Carrycase 4.5 £5.25
Andes To Amazon [DVD] 4.5 £12.95
Matthew Mohlke & Martin Strel The Man who Swam the Amazon: 3,274 Miles on the World's Deadliest River 4.0 £4.76
Hmm, those formatted prices look a bit dodgy. We'll have to do something about the encoding. Fortunately, we can easily fix this in the HTML.
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8"/>
</head>
<body>
...
Better now. We can even do Japanese text.
$pas->set_locale("jp");
Amazonオリジナル マグカップ 黒 5.0
水野 貴明 俺流amazonの作り方―Amazon Webサービス最新活用テクニック 4.5 ¥ 2,730
リボルテックダンボー・ミニ Amazon.co.jpボックスバージョン 4.5
Amazonベーシック 4.7GB 16倍速 DVD-R データ用 (100枚入 スピンドルケース) [フラストレーションフリーパッケージ(FFP)] 3.0 ¥ 1,635
松本 晃一 アマゾンの秘密──世界最大のネット書店はいかに日本で成功したか 4.0
初音ミク-Project DIVA2- 特典 ねんどろいどぷらす 「初音ミク Project DIVA」特典Ver チャーム(仮)付き ¥ 4,698
【Amazon.co.jp限定】(お徳用ボックス) ラーメンバラエティアソートセット(海鮮中華そば・とりがら醤油・みそ・北海道みそ・北海道味噌・北海道塩 各2個) 4.5 ¥ 1,680
Amazonオリジナル マグカップ 白 4.5
Amazonベーシック 700MB 52倍速 CD-R (50枚入 スピンドルケース) [フラストレーションフリーパッケージ(FFP)] 3.5 ¥ 849
並河祐貴 クラウド Amazon EC2/S3のすべて~実践者から学ぶ設計/構築/運用ノウハウ~ (ITpro BOOKs) 3.0 ¥ 2,940
It would be nice to have a "add to cart" button as well. Unfortunately, there's no easy way to do this with the information we get back from Amazon. There is help available however at
https://affiliate-program.amazon.com/gp/associates/help/t1/a10? -- we should wrap this in a function.
function addToCart($asin, $locale) {
switch($locale) {
case "us": $domain = "com"; break;
case "uk": $domain = "co.uk"; break;
case "de": $domain = "de"; break;
case "ca": $domain = "ca"; break;
case "fr": $domain = "fr"; break;
case "jp": $domain = "co.jp"; break;
}
return("<form method=\"GET\" action=\"http://www.amazon." . $domain . "/gp/aws/cart/add.html\">" .
"<input type=\"hidden\" name=\"AssociateTag\" value=\"your associate tag\"/>" .
"<input type=\"hidden\" name=\"SubscriptionId\" value=\"your subscription ID\"/>" .
"<input type=\"hidden\" name=\"ASIN.1\" value=\"". $asin ."\"/>" .
"<input type=\"hidden\" name=\"Quantity.1\" value=\"1\"/>" .
"<input type=\"submit\" name=\"add\" value=\"add to cart\">" .
"</form>");
}
Now we can do a simple
echo addToCart
($item->
ASIN,
"uk");
inside the loop (change locale as appropriate).
Building on this and wrapping some HTML around the results, maybe add some AJAX, you'd get something like
this?. Keep in mind though that the PAS license limits you to 1 request per second -- you have to take care that this is not exceeded in your application yourself.
There is one comment on this page. [Display comment]