Home » OpenBook WordPress Plugin

OpenBook Plugin: Grabbing the Book Data

29 May 2008 2 Comments
This entry is part 6 of 12 in the series Building OpenBook

OpenBook WordPress PluginLast 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.

Index

Series Navigation«OpenBook Plugin: Coding in PHPOpenBook Plugin: Let There Be Cover Images!»

Post to Twitter

2 Comments »

  • Paul Rodriguez said:

    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: “;

  • John said:

    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!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.