Thoughts, rants and commentary of a simple man

Getting books details by ISBN in PHP

Posted on April 4th, 2009 in Geek Stuff,PHP,Programming,Web Development

Sometimes I love being a geek. Today, as I set out to inventory my collection of almost 500 books or so, I wanted to find a fast way to get the information on the book I wanted based on the books ISBN. This was because I was already getting tired of typing five books into the inventory (yes, I am that lazy) and I really did not want to keep typing out the authors name, the title and the subtitle for each book.

So I went to ISBNdb.com and entered an ISBN thinking I would be able to just copy and paste the book information from the output. That turned out to be overwhelmingly difficult since their output of the search is really convoluted. I knew what I wanted to get and they didn’t offer that.

But they did offer an API and, after reading their docs, I realized that I could write a script that would take an ISBN and return to me the information I was looking for in a way that I could just copy and paste it. Remarkably, it was faster and easier than I thought it would be. I actually developed this little snippet, freely available for you here, in about 5 minutes. Hope it helps you in some way, if you are looking for such a thing.

NOTE: Before using this code make sure to sign up for your own API key. In order to do that you will need to register for an account with ISBNdb.com and then create a key. But as soon as you do you will have immediate access to their API. The API is simple, responding to the request with a simple XML output. Anyhow, without further ado, here is the little script I put together to fetch me the data I was looking for the way I wanted it.

<?php
/**
 * Set this value to your own API key
 */
$apikey = '12345678';
 
/**
 * Initialize this var for use when forms are not posted
 */
$isbn = null;
 
/**
 * Initialize the result set var
 */
$rs = null;
 
/**
 * See if the form is posted
 */
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  /**
   * Get the ISBN from the form
   * 
   * No, there is no validation on this. This 
   * was for me and I knew I would not be at
   * all trying to trick my own script. USE 
   * THIS AS IS AT YOUR OWN PERIL.
   */
  $isbn = $_POST['isbn'];
 
  /**
   * Get the result as a SimpleXML object
   */
  $rs = simplexml_load_file("http://isbndb.com/api/books.xml?access_key=$apikey&index1=isbn&value1=$isbn");
}
?>
<html>
<head><title>ISBN Check</title></head>
<body>
<form method="post" action="<?php echo basename(__FILE__) ?>">
  <p>
    Enter an ISBN:<br />
    <input type="text" size="40" name="isbn" id="isbn" value="<?php echo $isbn ?>" />
  </p>
  <p>
    <input type="submit" name="submit" value="Search ISBN" />
  </p>
</form>
<?php
/**
 * Only output more stuff if there is a result
 */
if ($rs) : ?>
<hr />
Results for ISBN: <?php echo $isbn ?>:
<?php 
/**
 * Results for a book are in $rs->BookList->BookData
 * 
 * You can see this using var_dump($rs);
 */ 
$book = $rs->BookList->BookData;
?>
<p>
  <strong><?php echo $book->Title; ?></strong><br />
  <?php echo !empty($book->TitleLong) ? $book->TitleLong . '<br />' : null; ?>
  <?php echo $book->AuthorsText; ?><br />
  <?php echo $book->PublisherText; ?><br />
  <?php echo $isbn; ?>
</p>
<?php endif; ?>
</body>
</html>

Typical output might look like (from one of the books I was checking on):


Results for ISBN: 0316116955:

The day the universe changed
James Burke
Boston : Little, Brown, c1985.
0316116955


I hope you enjoy this, if you happen to be looking for an ISBN search tool that you can run on your own. 😉

12 Responses to “Getting books details by ISBN in PHP”

  1. You are a dork.

  2. True, but for me it actually saved a bunch of time. Enter and ISBN and phpppptttt, out comes the information formatted the way I wanted it.

    Of course, I was actually smiling inside that I finally used my mad coding skillz for something useful. 😉

  3. Can you add the publisher? I would love to have a catalog of our books!

  4. Yeah, all of the following fields are available:

    • Title – The main title
    • TitleLong – The main title and the subtitle separated by a colon
    • AuthorsText – All authors, separated by a comma
    • PublisherText – The publisher
    • Summary – Not sure what this is, but it is available
    • Notes – Not sure what this is
    • UrlsText – Not sure what this is
    • AwardsText – Not sure what this is
    • Prices – A list of pricing information and stores

    They can be easily integrated into the script I put together. Have you gotten an API Key yet? If not, go get one then email me. I will walk you thought how to get this on your server to use it.

  5. Holy cow. This is just what I was looking for!
    Thanks for sharing this!

  6. Glad I could help. 🙂

  7. People who find this may also be interested in this solution (code available for download) which does something similar to this, but also can Save to a pipe delimited text file (for easy importing into BookHound, for example). It also describes how you can use a relatively inexpensive bar code scanner for the ISBN number input.

    God bless the internets.

  8. Thanks for the linkage.

  9. A simple and itnelleignt point, well made. Thanks!

  10. Robert,
    I would be interested in talking to you through email regarding your “Getting books details by ISBN in PHP” and discussing ways to access more fields or developing a similar PHP script.

    Please email me so we can discuss.

    Thanks,
    Jim

  11. Jim,
    An email has been sent.

  12. trivial to “protect” the input string.
    I changed your code to look like
    // convert to a number
    $isbn = intval(trim($_POST[‘isbn’]),10);
    // the standard says it must be 13 digits long if assigned after Jan 1, 2007.
    if (strlen($isbn) < 14) {
    /**
    * Get the result as a SimpleXML object
    */
    $rs = simplexml_load_file("http://isbndb.com/api/books.xml?access_key=$apikey&index1=isbn&value1=$isbn&quot;);
    }

    I think the above is enought to protect from any possible attacks.

Leave a Reply


Fatal error: Call to undefined function show_subscription_checkbox() in /home1/robertg2/public_html/wp-content/themes/dark-sky/comments.php on line 79