HOWTO Obtain metadata for a book given its ISBN using Amazon Web Services in PHP

17 Jul, 2008 — HOWTO, Release

This is a quick snippet I put together for an academic project. To be able to write this, I had to go through several documentation resources, for what is essentially a single web service method call. I figured it would help if I shared my PHP code.

<?php
/**
 * Query Amazon about a particular book by ISBN and obtain metadata.
 * The author disclaims all copyright and places this in the public domain.
 *
 * Amazon's Terms of Use for this service require you to:
 * - Send no more than 1 request every second
 * - Direct traffic to them in some way. You can use the URL provided in the
 *   resulting metadata to achieve this.
 */
class ISBN {
  function getMetadataFromIsbn($isbn) {
    // Get your own accesskey at http://aws.amazon.com/
    $awsAccessKeyID = 'YOUR_ACCESS_KEY_ID_HERE';
    $awsSecretKey = 'YOUR_SECRET_KEY_HERE';
    $awsAssociateTag = 'YOUR_ASSOCIATE_TAG_HERE';

    $host = 'ecs.amazonaws.com';
    $path = '/onca/xml';

    $args = array(
      'AssociateTag' => $awsAssociateTag,
      'AWSAccessKeyId' => $awsAccessKeyID,
      'IdType' => 'ISBN',
      'ItemId' => $isbn,
      'Operation' => 'ItemLookup',
      'ResponseGroup' => 'Medium',
      'SearchIndex' => 'Books',
      'Service' => 'AWSECommerceService',
      'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
      'Version'=> '2009-01-06'
    );

    ksort($args);
    $parts = array();
    foreach(array_keys($args) as $key) {
      $parts[] = $key . "=" . $args[$key];
    }

    // Construct the string to sign
    $stringToSign = "GET\n" . $host . "\n" . $path . "\n" . implode("&", $parts);
    $stringToSign = str_replace('+', '%20', $stringToSign);
    $stringToSign = str_replace(':', '%3A', $stringToSign);
    $stringToSign = str_replace(';', urlencode(';'), $stringToSign);

    // Sign the request
    $signature = hash_hmac("sha256", $stringToSign, $awsSecretKey, TRUE);

    // Base64 encode the signature and make it URL safe
    $signature = base64_encode($signature);
    $signature = str_replace('+', '%2B', $signature);
    $signature = str_replace('=', '%3D', $signature);

    // Construct the URL
    $url = 'http://' . $host . $path . '?' . implode("&", $parts) . "&Signature=" . $signature;
    $rawData = file_get_contents($url);

    $metadata = simplexml_load_string($rawData);
    if (isset($metadata->Items->Request->Errors)) {
      return $metadata->Items->Request->Errors;
    } else {
      return $metadata->Items->Item;
    }
  }
}
?>
  1. To Whom it May Concern:
    Thanks so much for posting this; setting the IdType=ISBN was what I needed. My college’s library web application is sweet now because when users fill out a web form for a library item, they just have to provide the ISBN field, and all the rest of the fields are auto filled.

    ~Alicia Girvin
    The College at Brockport (4/20/09)

    Alicia Girvin — April 20, 2009 @ 12:46 pm

  2. I know this a noob question but what exactly does this script return. I was hoping to return the book title from an isbn search but using this script I’m confused about how to return that.

    Any help would be greatly appreciated, John

    john — December 13, 2010 @ 8:44 am

  3. Thanks for the info. It is sharp & clear.
    Please, can you comment ashow to collect the data into a file e.g. Excel etc.
    does it provide links to the pictureqvisual of the book/product ?

    Any helpful comment will be appreciated.

    Izzy Keren — September 11, 2011 @ 11:21 pm

  4. Krishna — May 8, 2012 @ 2:26 am

  5. Please let me know how to read this information as an array.

    ST — May 13, 2012 @ 10:12 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment