When Amazon changed its Product Advertising API to require signing of requests, it rendered my Rated New Fiction and Similar Products pipes unusable. The benefit of doing the work in pipes is that you don’t have to have a server from which to run the script. However, as I mentioned in my post about Signing Amazon Requests, it seems like signing Amazon requests using Pipes would require an external service that applies SHA1 to a block of text. A commenter suggested trying YQL rather than Yahoo! Pipes, but I decided instead to just redo the work in PHP since a server would have to be involved anyway. The signed request code that I used in both these examples is from this blog post: Amazon® AWS HMAC signed request using PHP. Note that you have to put in your public and private keys to make it work.
Rated New Fiction
This script grabs the new fiction list from the King Library and then orders it based on the average rating and number of reviews from Amazon. See the results here.
<?php
/* ratedNewFiction.php
* The purpose of this script is to grab the new fiction feed
* from SJ Library, then do an Amazon query for each item and
* order them based on average rating and number of customer
* reviews for the item.
*
* Copyright 2009 Heather Devine
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The GNU General Public license can be found at
* <http://www.gnu.org/licenses/>.
*/
$amazonPublicKey = "YOUR_PUBLIC_KEY";
$amazonPrivateKey = "YOUR_PRIVATE_KEY";
// Set up our XML result document
header('Content-type: text/xml');
$xmlDoc = new DOMDocument("1.0");
$xmlDoc->formatOutput = true;
$root = $xmlDoc->createElement("rss");
$root->setAttribute("version", "2.0");
$xmlDoc->appendChild($root);
$channel = $xmlDoc->createElement("channel");
$root->appendChild($channel);
$newNode = $xmlDoc->createElement("title");
$newNode->appendChild($xmlDoc->createTextNode("Rated New Fiction Feed"));
$channel ->appendChild($newNode);
$newNode = $xmlDoc->createElement("link");
$newNode->appendChild($xmlDoc->createTextNode("http://www.flexyourinfo.com/projects/misc/ratedNewFiction.php"));
$channel ->appendChild($newNode);
$newNode = $xmlDoc->createElement("description");
$newNode->appendChild($xmlDoc->createTextNode("A list of the new fiction items at San Jose Library, ordered based on average rating and number of customer reviews from Amazon.com"));
$channel ->appendChild($newNode);
// Grab the new fiction feed
$fictionFeedDoc = new DOMDocument();
$fictionFeedDoc->load('http://mill1.sjlibrary.org/feeds/fiction.xml');
$fictionArr = array();
foreach ($fictionFeedDoc->getElementsByTagName('item') as $feedItem) {
// Extract the ISBN from the guid
$guid = $feedItem->getElementsByTagName('guid')->item(0)->nodeValue;
$pos = strpos($guid, " ");
if ($pos === false) { // Didn't find a space, so just use $guid
$isbn = $guid;
} else {
$isbn = substr($guid, 0, $pos);
}
// In the future, we could do some work to validate the ISBN
// here if we wanted to. For now, send the ISBN to Amazon.
$paramArray = array("SearchIndex"=>"Books", "ResponseGroup"=>"Large",
"Operation"=>"ItemLookup", "MerchantId"=>"Amazon",
"ItemId"=>$isbn, "IdType"=>"ISBN",
"ContentType"=>"text/xml");
$amazonURL = aws_signed_request("com", $paramArray, $amazonPublicKey, $amazonPrivateKey);
$amazonDoc = new DOMDocument();
$amazonDoc->load($amazonURL);
// Check to see if the request returned an error.
$errorNode = $amazonDoc->getElementsByTagName('Error');
if ($errorNode->length > 0) // There's an error... move along. Could add a debug flag and/or log the error somewhere.
continue;
// Get the customer rating (Items.Item.CustomerReviews.AverageRating)
// and construct the rating image URL
$averageRatingNode = $amazonDoc->getElementsByTagName('AverageRating');
if ($averageRatingNode->length == 0) { // No average rating
$averageRating = 0.0;
$ratingImg = "http://www.flexyourinfo.com/images/no-reviews.gif";
} else {
$averageRating = $averageRatingNode->item(0)->nodeValue;
$averageStr = str_replace(".", "-", $averageRating);
$ratingImg = "http://g-ecx.images-amazon.com/images/G/01/x-locale/common/customer-reviews/ratings/stars-" . $averageStr. "._V25749327_.gif";
}
// Get the number of reviews (Items.Item.CustomerReviews.TotalReviews)
$numReviews = 0;
$numReviewsNode = $amazonDoc->getElementsByTagName('TotalReviews');
if ($numReviewsNode->length > 0) {
$numReviews = $numReviewsNode->item(0)->nodeValue;
}
$itemDesc = $feedItem->getElementsByTagName('description')->item(0)->nodeValue;
$fullDesc = "Average rating: <img src=\"" . $ratingImg . "\"> (Reviews: " . $numReviews . ")<br/><br/>" . $itemDesc;
$feedRSS = array (
'rating' => $averageRating,
'reviews' => $numReviews,
'title' => $feedItem->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $feedItem->getElementsByTagName('link')->item(0)->nodeValue,
'description' => $fullDesc,
'guid' => $feedItem->getElementsByTagName('guid')->item(0)->nodeValue
);
array_push($fictionArr, $feedRSS);
}
// Sort the array
usort($fictionArr, "cmp");
// Display the output
foreach ($fictionArr as $item) {
$newItem = $xmlDoc->createElement("item");
$newNode = $xmlDoc->createElement("title");
$newNode->appendChild($xmlDoc->createTextNode($item['title']));
$newItem->appendChild($newNode);
$newNode = $xmlDoc->createElement("link");
$newNode->appendChild($xmlDoc->createTextNode($item['link']));
$newItem->appendChild($newNode);
$newNode = $xmlDoc->createElement("description");
$newNode->appendChild($xmlDoc->createTextNode($item['description']));
$newItem->appendChild($newNode);
$newNode = $xmlDoc->createElement("guid");
$newNode->setAttribute("isPermalink", "false");
$newNode->appendChild($xmlDoc->createTextNode($item['guid']));
$newItem->appendChild($newNode);
$channel->appendChild($newItem);
}
// Display the XML
print $xmlDoc->saveXml();
function cmp($a, $b){
if ($a[rating] == $b[rating]) {
if ($a[reviews] == $b[reviews]) {
return 0;
}
elseif ($a[reviews] > $b[reviews]) {
return -1;
}
elseif ($a[reviews] < $b[reviews]) {
return 1;
}
}
elseif ($a[rating] > $b[rating]) {
return -1;
}
elseif ($a[rating] < $b[rating]) {
return 1;
}
}
// aws_signed_request function modified from version
// created by Ulrich Mierendorff, found here: http://www.mierendo.com/software/aws_signed_query/
// Change is to provide the URL without loading the XML
function aws_signed_request($region, $params, $public_key, $private_key)
{
/*
Original version Copyright (c) 2009 Ulrich Mierendorff
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER EALINGS IN THE SOFTWARE.
*/
/*
Parameters:
$region - the Amazon(r) region (ca,com,co.uk,de,fr,jp)
$params - an array of parameters, eg. array("Operation"=>"ItemLookup",
"ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
$public_key - your "Access Key ID"
$private_key - your "Secret Access Key"
*/
// some paramters
$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";
// additional parameters
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
// GMT timestamp
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
// API version
$params["Version"] = "2009-03-31";
// sort the parameters
ksort($params);
// create the canonicalized query
$canonicalized_query = array();
foreach ($params as $param=>$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
// create the string to sign
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
// calculate HMAC with SHA256 and base64-encoding
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
// encode the signature for the request
$signature = str_replace("%7E", "~", rawurlencode($signature));
// create request
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
return $request;
}
?>
Similar Products
This script takes an ISBN number and provides information about the book as well as up to 5 similar products. See the results here.
<?php
/* similarProducts.php
* The purpose of this script is to return a list of up to 5
* similar products. It returns the average rating and number
* of reviews for a book, as well as the title, book cover,
* rating, and ISBN of up to 5 similar products.
*
* Copyright 2009 Heather Devine
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The GNU General Public license can be found at
* <http://www.gnu.org/licenses/>.
*/
$amazonPublicKey = "YOUR_PUBLIC_KEY"
$amazonPrivateKey = "YOUR_PRIVATE_KEY";
// Set up our XML result document
header('Content-type: text/xml');
$xmlDoc = new DOMDocument("1.0");
$xmlDoc->formatOutput = true;
$root = $xmlDoc->createElement("similarItems");
$xmlDoc->appendChild($root);
$newNode = $xmlDoc->createElement("title");
$newNode->appendChild($xmlDoc->createTextNode("Similar Products"));
$root->appendChild($newNode);
$newNode = $xmlDoc->createElement("link");
$newNode->appendChild($xmlDoc->createTextNode("http://www.flexyourinfo.com/projects/misc/similarProducts.php"));
$root->appendChild($newNode);
$newNode = $xmlDoc->createElement("description");
$newNode->appendChild($xmlDoc->createTextNode("Information about an item and up to 5 similar products"));
$root->appendChild($newNode);
$items = $xmlDoc->createElement("items");
$root->appendChild($items);
$isbn = "0451462564"; // Could get this from a parameter passed into the PHP
$fictionArr = array();
$paramArray = array("SearchIndex"=>"Books", "ResponseGroup"=>"Large",
"Operation"=>"ItemLookup", "MerchantId"=>"Amazon",
"ItemId"=>$isbn, "IdType"=>"ISBN",
"ContentType"=>"text/xml");
$amazonURL = aws_signed_request("com", $paramArray, $amazonPublicKey, $amazonPrivateKey);
$amazonDoc = new DOMDocument();
$amazonDoc ->load($amazonURL);
// Check to see if the request returned an error.
$errorNode = $amazonDoc->getElementsByTagName('Error');
if ($errorNode->length > 0) { // There's an error finding the item requested. Could include the error and code from AWS in future.
$newItem = $xmlDoc->createElement("item");
$newNode = $xmlDoc->createElement("title");
$newNode->appendChild($xmlDoc->createTextNode("Error"));
$newItem->appendChild($newNode);
$newNode = $xmlDoc->createElement("description");
$newNode->appendChild($xmlDoc->createTextNode("Unable to find information for item with ISBN $isbn"));
$newItem->appendChild($newNode);
$items->appendChild($newItem);
print $allDoc->saveXml();
exit;
}
$info = itemEntry($amazonDoc);
array_push($fictionArr, $info);
// Now get the similar items
$similarASIN = array();
$similarProductNode = $amazonDoc->getElementsByTagName('SimilarProduct');
if ($similarProductNode->length > 0) { // No similar products
for ($i = 0; $i < $similarProductNode->length; $i++) {
$asinNode = $similarProductNode->item($i)->getElementsByTagName('ASIN');
$asin = $asinNode->item(0)->nodeValue;
array_push($similarASIN, $asinNode->item(0)->nodeValue);
}
}
foreach ($similarASIN as $asin) {
$paramArray = array("ResponseGroup"=>"Large",
"Operation"=>"ItemLookup", "MerchantId"=>"Amazon",
"ItemId"=>$asin, "IdType"=>"ASIN",
"ContentType"=>"text/xml");
$amazonURL = aws_signed_request("com", $paramArray, $amazonPublicKey, $amazonPrivateKey);
$amazonDoc = new DOMDocument();
$amazonDoc ->load($amazonURL);
// Check to see if the request returned an error.
$errorNode = $amazonDoc->getElementsByTagName('Error');
if ($errorNode->length == 0) { // No error, keep going
$info = itemEntry($amazonDoc);
array_push($fictionArr, $info);
}
}
// Display the output
foreach ($fictionArr as $item) {
$newItem = $xmlDoc->createElement("item");
$newNode = $xmlDoc->createElement("title");
$newNode->appendChild($xmlDoc->createTextNode($item['title']));
$newItem->appendChild($newNode);
$newNode = $xmlDoc->createElement("rating");
$ratingInfo = "Average Rating: " . $item['rating'] . " (" . $item['reviews'] . " reviews)";
$newNode->appendChild($xmlDoc->createTextNode($ratingInfo));
$newNode->setAttribute("img", $item['ratingImg']);
$newItem->appendChild($newNode);
$newNode = $xmlDoc->createElement("isbn");
$newNode->appendChild($xmlDoc->createTextNode($item['isbn']));
$newItem->appendChild($newNode);
$newNode = $xmlDoc->createElement("cover");
$newNode->setAttribute("url", $item['url']);
$newNode->setAttribute("width", $item['width']);
$newNode->setAttribute("height", $item['height']);
$newNode->setAttribute("type", "image/jpeg");
$newItem->appendChild($newNode);
$items->appendChild($newItem);
}
// Display the XML
print $xmlDoc->saveXml();
function itemEntry($doc)
{
// Get the customer rating (Items.Item.CustomerReviews.AverageRating)
// and construct the rating image URL
$averageRatingNode = $doc->getElementsByTagName('AverageRating');
if ($averageRatingNode->length == 0) { // No average rating
$averageRating = 0.0;
$ratingImg = "http://www.flexyourinfo.com/images/no-reviews.gif";
} else {
$averageRating = $averageRatingNode->item(0)->nodeValue;
$averageStr = str_replace(".", "-", $averageRating);
$ratingImg = "http://g-ecx.images-amazon.com/images/G/01/x-locale/common/customer-reviews/ratings/stars-" . $averageStr. "._V25749327_.gif";
}
// Get the number of reviews (Items.Item.CustomerReviews.TotalReviews)
$numReviews = 0;
$numReviewsNode = $doc->getElementsByTagName('TotalReviews');
if ($numReviewsNode->length > 0) {
$numReviews = $numReviewsNode->item(0)->nodeValue;
}
$itemAttr = $doc->getElementsByTagName('ItemAttributes');
$itemTitleNode = $itemAttr->item(0)->getElementsByTagName('Title');
$isbnNode = $itemAttr->item(0)->getElementsByTagName('ISBN');
$imgAttr = $doc->getElementsByTagName('SmallImage');
$urlNode = $imgAttr->item(0)->getElementsByTagName('URL');
$widthNode = $imgAttr->item(0)->getElementsByTagName('Width');
$heightNode = $imgAttr->item(0)->getElementsByTagName('Height');
$itemInfo = array (
'title' => $itemTitleNode->item(0)->nodeValue,
'rating' => $averageRating,
'ratingImg' => $ratingImg,
'reviews' => $numReviews,
'isbn' => $isbnNode->item(0)->nodeValue,
'url' => $urlNode->item(0)->nodeValue,
'width' => $widthNode->item(0)->nodeValue,
'height' => $heightNode->item(0)->nodeValue,
);
return $itemInfo;
}
// aws_signed_request function modified from version
// created by Ulrich Mierendorff, found here: http://www.mierendo.com/software/aws_signed_query/
// Change is to provide the URL without loading the XML
function aws_signed_request($region, $params, $public_key, $private_key)
{
/*
Original version Copyright (c) 2009 Ulrich Mierendorff
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER EALINGS IN THE SOFTWARE.
*/
/*
Parameters:
$region - the Amazon(r) region (ca,com,co.uk,de,fr,jp)
$params - an array of parameters, eg. array("Operation"=>"ItemLookup",
"ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
$public_key - your "Access Key ID"
$private_key - your "Secret Access Key"
*/
// some paramters
$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";
// additional parameters
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
// GMT timestamp
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
// API version
$params["Version"] = "2009-03-31";
// sort the parameters
ksort($params);
// create the canonicalized query
$canonicalized_query = array();
foreach ($params as $param=>$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
// create the string to sign
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
// calculate HMAC with SHA256 and base64-encoding
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
// encode the signature for the request
$signature = str_replace("%7E", "~", rawurlencode($signature));
// create request
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
return $request;
}
?>
Any thoughts or suggestions on these scripts are welcome. I also wrote a script for an idea someone sent to me; I think I’m going to plug it into a Flex project and do a step-by-step explanation of that for a future post. Incidentally, that post probably won’t be for a couple weeks: on Saturday, I’m flying to Oklahoma to spend some time with family, do some genealogy research, and attend the annual Eastern Shawnee Powwow. Hopefully this year the weather will be nice!
My name is Heather and I ♥ monkeys. I am a computer scientist in San Jose and my background is in cognitive science, computer science, usability, and library and information science. My interests include preservation, oral history, indigenous knowledge and technology. 
It‘s quiet in here! Why not leave a response?