Some months ago I added book reviews from Amazon to My Reading List. Under Reviews, when you click "Amazon" you get an overlay with the 3 most useful reviews. In this post I show you how to do it.

Amazon ECS

featured image

I use Amazon all the time to look if a particular book is worth my time. Hence book reviews had to be in this app. I found a good PHP class to do this easily: Amazon-ECS-PHP-Library. Using this class the app generates an URL to an Amazon page with only the reviews on it (e.g. http://www.amazon.com/reviews/iframe?akid=..more..parameters..), based on its ISBN number. This URL is linked to under Reviews: "Amazon". When clicked, I use Fancybox (discussed previously) to load the URL in an overlay (iframe). See the images below.

Inputs: ISBN (10 / 13 digits) and ASIN

Note that My Reading List uses the Google Books API which returns short and long ISBN numbers (10 and 13 digits respectively, about ISBN). The 10 digit string is usually equal to the ASIN which is the "Amazon Standard Identification Number". If there is no response from the short ISBN/ ASIN, I convert the longer ISBN to ASIN and try with that one. I had to add the "lookupIsbn" method to the Amazon ECS class to be able to do this. With the two ISBN types supported I got a response URL for almost all the books I have tested so far.

Caching

I was considering caching the Amazon iframe URLs, but they are only valid for 24 hours so I decided to generate them on the fly for now.

How it looks

This is the Practical Vim page:

Link to reviews:

link on book page

Overlay when clicked:

reviews in overlay

The code

# see comment above about 10 and 13 digit ISBNs 
if($reviews = getAmazonReviews($book['ISBN_10'])) {
  $amazonReviewsIframe = $reviews;
} else {
  $asin = isbnToAsin($book['ISBN_13']);
  $amazonReviewsIframe = getAmazonReviews($asin); 
}

..

if($amazonReviewsIframe) {
  echo "<a class='fboxEdit' href='$amazonReviewsIframe'>Amazon</a>";
}



(functions) 

# get the first two values from the Product Advertising API, 
# last 2 values are needed for Amazon-ECS-PHP-Library
define("API_KEY",         "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
define("API_SECRET",      "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
define("COUNTRY_CODE",    "com");
define("ASSOC_TAG",       "myrealis");


function amazonApi() {
  # https://github.com/Exeu/Amazon-ECS-PHP-Library/blob/master/lib/AmazonECS.class.php
  require_once 'AmazonECS.class.php'; 
  $client = new AmazonECS(API_KEY, API_SECRET, COUNTRY_CODE, ASSOC_TAG);                                                                
  return $client;
}

function getAmazonReviews($asin) {
  $client = amazonApi();
  $response = $client->category('Books')->responseGroup('Reviews')->search($asin);

  if($response->Items->Item->ASIN == $asin) {
    return $response->Items->Item->CustomerReviews->IFrameURL;
  } else {
    return False;
  }
}

function isbnToAsin($isbn){
  $client = amazonApi();

  # I extended the AmazonECS.class with the "lookupIsbn" function
  $response  = $client->category('Books')->lookupIsbn($isbn);

  if(isset($response->Items->Item->ASIN)) {
    return $response->Items->Item->ASIN;
  } else {
    return False;
  }
}


# added inside the AmazonECS.class
..
public function lookupIsbn($isbn) {
  $params = $this->buildRequestParams('ItemLookup', array(
    'ItemId' => $isbn, 'IdType' => 'ISBN', 'SearchIndex' => 'Books'
  ));

  return $this->returnData(
    $this->performSoapRequest("ItemLookup", $params)
  );
}


Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me