Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i parse Json data from openlibrary api? (properly)

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.

<?php
$url ="https://openlibrary.org/api/books?bibkeys=ISBN:0789721813&jscmd=details&format=json";

$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);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);

$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]);
 }

curl_close($cURL);
?>

When the page is loaded the following is displayed:

ISBN: 0789721813 title: Red Hat Linux author: Hellums, Duane
like image 441
ben jay hutton Avatar asked Oct 31 '22 00:10

ben jay hutton


2 Answers

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:

curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);

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]);
}
like image 99
Casimir et Hippolyte Avatar answered Nov 12 '22 12:11

Casimir et Hippolyte


this will maybe help

echo 'title : '.$book['details']['title'].'<br />';
echo 'subtitle : '.$book['details']['subtitle'].'<br />';
echo 'lc_classifications : '.$book['details']['lc_classifications'][0].'<br />';
echo 'latest_revision : '.$book['details']['latest_revision'].'<br />';
echo 'edition_name : '.$book['details']['edition_name'].'<br />';
echo 'languages : '.$book['details']['languages'][0]['key'].'<br />';
echo 'subjects : '.$book['details']['subjects'][0].'<br />';
echo 'location : '.$book['details']['location'][0].'<br />';
echo 'type : '.$book['details']['type']['key'].'<br />';
echo 'publish_country : '.$book['details']['publish_country'].'<br />';
echo 'other_titles : '.$book['details']['other_titles'][0].'<br />';
echo 'publishers : '.$book['details']['publishers'][0].'<br />';
echo 'last_modified : '.$book['details']['last_modified']['value'].'<br />';
echo 'key : '.$book['details']['key'].'<br />';
echo 'authors : '.$book['details']['authors'][0]['name'].'<br />';
echo 'publish_places : '.$book['details']['publish_places'][0].'<br />';
echo 'pagination : '.$book['details']['pagination'].'<br />';
echo 'works : '.$book['details']['works'][0]['key'].'<br />';
echo 'isbn 10 : '.$book['details']['isbn_10'][0].'<br />';
like image 20
bat .t Avatar answered Nov 12 '22 14:11

bat .t