How can I analyze Json data from openlibrary api? (correctly)


Forgive me if this has been answered. I have seen various answer regarding json data and openlibrary

So far the json data I am getting from openlibrary and the json data i see used in examples seem to differ in the format

My question is, Using php (or javascript) how do i get the data into an array or indavidual variables and put them into a mysql database.

  • addition to previous question - I would like to display the raw data below as:

Title: Tile of book Author: Author of book Isbn: Isbn number etc.

and then put these details into a mysql data base

[Update 2015-011-07] Now I have recieved the answer, I have updated the code below to show how it should be. The following will request json data from openlibrary and it will be returned as a string. the ISBN number in $url is just for testing purposes, so by all means change it.

$url ="";

$headers = array(
    "Content-type: application/json;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: \"run\""

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($cURL);

foreach (json_decode($result, true) as $book)
   printf("\nISBN: %s\ttitle: %s\tauthor: %s", $book['details']['isbn_10'][0], $book['details']['title'], $book['details']['contributions'][0]);


When the page is loaded the following is displayed:

ISBN: 0789721813 title: Red Hat Linux author: Hellums, Duane

By default, cURL automatically output the transfer. Your code only displays the json content, but curl_exec($cURL) returns 1 or 0 if something gets wrong, and not the json content. That's why you are unable to get the array or object you want with json_decode, the JSON string is not in the $result variable.

To obtain what you want, you need to set an other cURL option:


In this way curl_exec($cURL) will return the transfer as a string and will no more output it automatically.

See the PHP manual about the returned values of curl_exec.

Then you only need to use json_decode:

foreach (json_decode($result, true) as $book) {
    printf("\nISBN: %s\ttitle: %s\tauthor: %s", $book['details']['isbn_10'][0], $book['details']['title'], $book['details']['contributions'][0]);