How To Get a Book’s Info From Its ISBN

I recommend you know  How To Parse XML for this tutorial. The second thing we are going to need is an API key from the free ISBN database.

Now that you have the access key let’s set up the url we’ll call to give us the book info.

The base url is

http://isbndb.com/api/books.xml?

For the purposes of this tutorial we are going to have to pass three paramaters, “access_key” (api key), “results” and “index1”.

access_key=API_KEY

Replace “API_Key” with the api key you got.

results=details

The value “details” just means that we want the book’s detailed information. There are different types of results such as “prices” that will give you any books prize on different stores.

index1=isbn

Tells the ISBN database that we will be searching by ISBN. Replacing the value “isbn” with “title” will search by title but replacing it with “author” or “publisher” will not work, I will show you how to search by those and more on another post, and also how to use different result sets.

value1=xxxxxxxxxx

The search query, in this case “xxxxxxxxxx”, is an ISBN number of your choice, it can be either an ISBN10 or 13 and it can include dashes.

Now that we know the structure of this API’s URL let’s see how it looks when put together. Remember to replace the value of access key with your API key and use the same ISBN I use just so we can be on the same page.

http://isbndb.com/api/books.xml?access_key=API_KEY&results=details&index1=isbn&value1=0596007736

We don’t need a script to see the response we’ll get from this url, simply put it in your browser’s address bar. If you are using Chrome I recommend you use FireFox or even IE8 *gasp* for this part of the tutorial.

Response in FireFox 3.5 for ISBN 0596007736

Response in FireFox 3.5 for ISBN 0596007736

Now it’s just a matter of parsing the response using the function “simplexml_load_file”, but before we do that let’s talk about a few things you have to know from the response.

The first thing you should notice is the “total_results” attribute for the BookList tag, as you might guess if the value of this attribute is zero it means that no book was found. The second thing is the two title tags <Title> and <TitleLong>. It’s important to know that the response you get might have one, the other, or sometimes both types of titles. I prefer to use TitleLong when it’s avaible because from what I’ve seen it’s more descriptive, and can include other media that comes with the book such as CD’s and/or DVD’s , but nothing keeps you from using both tags however you wish.

Let’s build a simple script that will call the API and shows you the results after they have been formated. Am going to create three files “find-book.html”, “find-book.js” and “find-book.php” , the file you should pay attention to the most is “find-book.php” , if you don’t know what’s going on in “find-book.js”  I invite you to read my tutorial PHP AJAX Contact Form , find-book.js (javascript) will be used to pass the paramater “isbn” to the php file and get the response using AJAX so this file (find-book.js) is not necessary but it makes things look nicer.

find-book.html contents.

We just need a text field, a button and a division where we will see the book’s information response from find-book.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<label>Type an ISBN and click the button to get the information</label><br/>
<input type="text" id="isbn" /><br/>
<button id="getBook">Get Info</button>
<div id="bookInfo">

</div>
<!-- Include Jquery-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<!-- Include find-book.js -->
<script type="text/javascript" src="find-book.js"></script>
</body>
</html>

find-book.js contents

$(document).ready(function(){
var isbn;
  $("#getBook").click(function(){ // when you click the button
    // fade out bookinfo div
	$("#bookInfo").fadeOut("slow");
	// assign isbn value to var isbn
    isbn=$("#isbn").val();
	// send isbn value as POST variable "isbn" and load result in bookinfo div
    $("#bookInfo").load("find-book.php",{isbn:isbn},function(){
	    // fade in bookinfo div
	 	$("#bookInfo").fadeIn("slow");
	});
  });
});

find-book.php contents

We need to get the variable from find-book.html we sent with find-book.js and assign it to $isbn.

<?php
// get isbn
$isbn=$_POST['isbn'];
// create url
$url='http://isbndb.com/api/books.xml?access_key=xxxxxx&results=details&index1=isbn&value1='.$isbn;
// load url into $response
$response=simplexml_load_file($url);

// check if we got at least one result
if($response->BookList['total_results']>0){
 // we got at least one result

// assign each book to $book
 foreach($response->BookList->BookData as $book)
 {
   echo "Short Title: {$book->Title}<br/>
   Long Title: {$book->TitleLong}<br/>
   Author(s): {$book->AuthorsText}<br />
   Publisher: {$book->PublisherText}<br/>
   ISBN10: {$book['isbn']}<br/>
   ISBN13: {$book['isbn13']}<br/>
   Edition Information: {$book->Details['edition_info']}<br/>
   Language: {$book->Details['language']}<br/>
   Physical Description: {$book->Details['physical_description_text']}
  "
 }
}
else
{
echo 'No book was found with ISBN: '.$isbn;
}

?>

That’s all I’ve got for this tutorial, check out the working example and leave a comment below.