OpenBook Plugin: Grabbing the Book Data
- When You Link to a Book, Do You Link to Amazon, LibraryThing, WorldCat, or What?
- Four Big Libraries: I Pick OpenLibrary
- OpenBook Plugin: Requirements
- OpenBook Plugin: The Open Library API
- OpenBook Plugin: Coding in PHP
- OpenBook Plugin: Grabbing the Book Data
- OpenBook Plugin: Let There Be Cover Images!
- OpenBook Plugin: The Importance of Linking to Publisher Websites
- OpenBook Plugin: Graphic Design
- OpenBook Plugin: Hooking into WordPress
- OpenBook Plugin: Now Accepting Arguments, and Beta Testers
- OpenBook Beta Release!
Last time, I pulled the book data from OpenLibrary into PHP so that I could start programming it. The data is fairly extensive but I just want a few fields, as per the requirements: author, book title, and publisher. Ideally, I would like to get a book cover image too — more on that later.
I have pulled all the data into a single variable, $bookdata. It may look a little confusing, but the good folks at OpenLibrary have put it in a standard format, JSON (JavaScript Object Notation). This means I can safely parse the data. But life gets even easier. PHP version 5 comes with a JSON library that makes extracting data very easy. My web server (netfirms.ca) uses PHP 4 by default, but a small switch in my website’s control panel upgraded it to PHP 5.
The fields I want are in the “results” section of the $bookdata variable, so I use the JSON library to extract the results, like so:
$obj = json_decode($bookdata );
$result = $obj->{‘result’};
Next I grab the title (and subtitle), authors (under “contributions”), and publisher.
$title = $result ->{‘title’};
$subtitle = $result ->{’subtitle’};
$contributions= $result ->{‘contributions’};
$publishers= $result ->{‘publishers’};
Notice there is no URL for the cover image in the results. I was disappointed about that. Very much so, because I want a cover image to display on the posts. I have a plan to resolve this, but that deserves a post all to itself, and that will have to wait a little while because I have to do some research and proof-of-concept work. Once I have that sorted, I will format all the book data into its final HTML. For now I will just print them out, using the PHP echo:
echo “Title: “.$title;
echo “Subtitle: “.$subtitle;
echo “Author 1: “.$contributions[0];
echo “Author 2: “.$contributions[1];
echo “Publisher: “.$publishers[0];
When I wrap everything so far in PHP tags, the code is processed, and the results are displayed like:
Title: Revolting librarians redux
Subtitle: radical librarians speak out
Author 1: Roberto, Katia, 1975-
Author 2: West, Jessamyn, 1968-
Publisher: McFarland & Co.
I’ll be back after I get the book cover image matter sorted.

Two small suggestions. If you write
echo “Title:” . ucwords($title);
It will return with proper capitalization:
Title: Revolting Librarians Redux
For the cover, OpenLibrary seems to use a fairly consistent system. I presume they will eventually integrate the cover URLs into their API, but until then, you could use:
$full_cover_url = “http://openlibrary.org/static/bookcovers/full/” . substr($result->{‘isbn_10′}, 0, 1} . “/” . substr($result->{‘isbn-10′}, 1, 1) . “/” . $result->{‘isbn_10′} . “.jpg”;
$thumb_cover_url = “http://openlibrary.org/static/bookcovers/thumb/” . substr($result->{‘isbn_10′}, 0, 1} . “/” . substr($result->{‘isbn-10′}, 1, 1) . “/” . $result->{‘isbn_10′} . “.jpg”;
echo “Full cover: “;
echo “Thumbnail cover: “;
Paul, I knew there was a good reason for blogging this process. I will definitely use your first suggestion, and I will have a good look at the second. OL is working on a cover image API, but till then your suggestion might work. Much appreciated.
Leave your response!
Slow Reading
Available from Litwin Books | Read a chapter online
OpenBook WordPress Plugin
Inserts a book cover image, title, author, and other book data from Open Library into a WordPress post.
Download the Plugin | Read More | Report an Issue | Join the Discussion List
Series
Other posts belonging to this series
What I’m Reading
Also Reading