Using PEAR and AWS to keep an eye on Amazon
What could possibly be better for a writer's ego other than being read and being praised? Hmm…
So I wanted to have a page that shows the books I've written, together with their Amazon sales rank and the average customer rating and number of reviews. It's really easy. I took one example out of the PEAR book and slightly modified it.
Implementation at a glance
- Get an Amazon Web Services (AWS) subscription id
- Get a copy of PEAR::Services_Amazon package
- Make a request
- Display the response
When making a request, you need to say what type of request it is and what type of response you want. It's documented here. For the type of request, look under "API reference -> Operations" and for the response type, look under "API reference -> Response Groups"
The code
// include the PEAR package require_once 'Services/AmazonECS4.php'; // Your AWS subscription id $subscriptionId = '1WQDAES5PQ**********'; // create a new client by supplying // subscription id $amazon = new Services_AmazonECS4($subscriptionId); $amazon->setLocale('US'); // output options // what do you need returned? $options = array(); $options['ResponseGroup'] = 'SalesRank,ItemAttributes,Reviews'; // for which books // comma-delimited list of ISBNs $items = '1904811795,1904811914,1904811132'; // do the request $result = $amazon->ItemLookup($items, $options); // check for errors if (PEAR::isError($result)) { print "An error occured<br/>"; print $result->getMessage() . "<br/>"; exit(); } // some spaghetti to display HTML response echo '<ul>'; foreach ($result['Item'] as $book) { // loop the books // URL echo '<li><a href="'. $book['DetailPageURL'] .'">'; // book title echo $book['ItemAttributes']['Title'], '</a><br />'; // authors, comma-delimited echo implode(', ',$book['ItemAttributes']['Author']); // sales rank echo '<br />Sales rank: ', $book['SalesRank']; // average rating if (!empty($book['CustomerReviews'])) { echo '<br />Rating: '; echo $book['CustomerReviews']['AverageRating']; echo ', based on '; echo $book['CustomerReviews']['TotalReviews'], ' reviews'; } echo '</li>'; } echo '</ul>'; <a href="#" onclick="javascript:document.getElementById('response').style.display='block';" >Show complete response</a> <pre id="response" style="display: none"> print_r($result); </pre>
Post this entry to: » del.icio.us » Digg » Furl » Newsvine » reddit » Y!
January 11th, 2007 at 2:34 am
Interesting. Heads up though: I'm getting a 404 from your full response link http://www.phpied.com/files/amazon/response
January 11th, 2007 at 2:34 am
Sorry, I had NoScript running as usual.
January 11th, 2007 at 8:05 am
Stoyan Stefanov's Blog: Using PEAR and AWS to keep an eye on Amazon…
…
January 17th, 2007 at 1:34 am
Very cool!
I also had a similar urge to check Amazon Sales Rank every hour for my newly published book. So I ended up picking Sales Rank tracking as an usecase for my next hobby project: Charteous, a website devoted to charting that tracks Amazon Sales Rank and shows the results as nice charts. Interestingly, I also chose PHP as the main development language.
/Pansy
January 17th, 2007 at 8:56 am
Thanks Pansy, charteo.us is really cool, with this slider in the graphs? I couldn't see which one is your book though.
rankforest.com is a similar service, if you haven't seen it. Here's an example using my phpBB title:
http://rankforest.com/detail/1904811132/30
January 23rd, 2007 at 7:01 am
Thanks for the script, helped me a lot!!!
November 16th, 2007 at 11:20 am
Could PEAR be used to access Amazon purchases information for my Seller Central account? I have looked at their Event Notification Service and it sucks !!