My so-called Playground

Google Book Search API with Code Igniter

June 28th, 2009 Posted in PHP

I was working on a project, an e-commerce site where you can buy and/or sell books online. SellingĀ  was a bit simple, students can add books by entering the ISBN and get its information from an API and for this, I was asked to use Amazon API. I spent a couple of hours, maybe a whole day trying to figure out why the sample codes i was testing won’t work. Then i learned that Amazon API (Alexa Web Search) was shut down by Amazon itself, that’s why it’s not returning any results. And so we decided to use google book api instead. Below is the code on how to retrieve some book information by ISBN using code igniter and zend framework through the google book api:

// This code is a method in my controller
// and called through ajax(using jquery)
// and returns result in json format

// I also created a library so i can easily load the
// zend framework and attain a neat code as possible

$isbn = $this->input->post("isbn");

// we need to first load the zend
// library, specifically the Books library
$this->load->library('zend', 'Zend/Gdata/Books');
$this->zend->load('Zend/Gdata/Books');

// create new instance of the zend books object
$books = new Zend_Gdata_Books();

// we will query book information by passing a
// query string to google api which looks like this
// http://books.google.com/books?isbn:0738531367

$query = $books->newVolumeQuery();
$query->setQuery("isbn:$isbn");

// the code below invokes
// the query and gets the result
$feed = $books->getVolumeFeed($query);

// this is a flag, so obviously when no result is returned,
// 0 will trigger that on my front end
$data['result'] = "0";
foreach ($feed as $entry):

    // some entry was found
	$data['result'] = "1";
    // some of the entry results are returned through
    // an array object, so to easily extract

    // just implode it and separate by a
    // character, in my case i used a comma(,)
	$data['authors'] = implode(", ",$entry->getCreators());
	$data['description'] = implode(", ",$entry->getDescriptions());
	$data['publishers'] = implode(', ',$entry->getPublishers());
	$data['identifiers'] = implode(', ',$entry->getIdentifiers());
	$data['category'] = implode(",", $entry->getSubjects());
	$data['book_title'] = $entry->getTitle()->text;
	$x = $entry->getThumbnailLink();
    // there are times that $entry->getThumbnailLink() returns
    // a null value, thus $x below is not an object
    // so use @ to escape possible errors in the codes below
    $data['book_image'] = @$x->href;
	$data['volume_id'] = $entry->getVolumeId();
	$data['year_published'] = implode(',' , $entry->getDates());
	$x = $entry->getFormats();
	$pages = @$x[0];
	$type = @$x[1];
	$data['pages']	= $pages->text;

	$x = $entry->getIdentifiers();
	$data['isbn10'] = str_replace("ISBN:", "",@$x[1]->text);
	$data['isbn13'] = str_replace("ISBN:", "",@$x[2]->text);

endforeach;		

// return result in json format
echo json_encode($data);

I will try to post soon on how to setup the Zend Framework library.

  1. 1 Trackback(s)

  2. Jun 29, 2009: News Google Book Search API with Code Igniter | Web 2.0 Designer

Post a Comment