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.

The result is here.

Implementation at a glance

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

<?php
 
// 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">
<?php print_r($result); ?>
</pre>

This entry was posted on Wednesday, January 10th, 2007 and is filed under AWS, PEAR, php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

8 Responses to “Using PEAR and AWS to keep an eye on Amazon”

  1. BillyG Says:

    Interesting. Heads up though: I’m getting a 404 from your full response link http://www.phpied.com/files/amazon/response

  2. BillyG Says:

    Sorry, I had NoScript running as usual.

  3. PHPDeveloper.org Says:

    Stoyan Stefanov’s Blog: Using PEAR and AWS to keep an eye on Amazon…

  4. Pansy Says:

    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

  5. Stoyan Says:

    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

  6. marcus Says:

    Thanks for the script, helped me a lot!!!

  7. Martin Says:

    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 !!

  8. S J H Says:

    Hi, just implemented this and you need the new updates for anybody who is wondering.

    http://wiki.php.net/pear/packages/services_amazon

    Look at the above link and should show you the new way, AmazonECS4.php is now depreciated.

    Cheers!

    http://www.booglam.com

Leave a Reply