Using PEAR and AWS to keep an eye on Amazon

January 10th, 2007. Tagged: AWS, PEAR, php

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

// 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);
// 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/>";
// 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>';
  >Show complete response</a>
<pre id="response" style="display: none">
<?php print_r($result); ?>

Tell your friends about this post on Facebook and Twitter

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov