How to parse Json data from openlibrary api? (properly)

Forgive me if this was answered. I saw various answers to json data and openlibrary

So far, the json data that I get from openlibrary and the json data that I see in the examples look in the format

My question is: use php (or javascript), how can I get the data in an array or indavidual variables and put them in the mysql database.

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

Title: Book Tile Author: Isbn Book Author: Isbn Number, etc.

and then put this data in mysql database

[Update 2015-011-07] Now I got a response, I 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 for testing purposes only, so be sure to replace 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 loads, the following is displayed:

 ISBN: 0789721813 title: Red Hat Linux author: Hellums, Duane 
+5
source share
2 answers

By default, cURL will automatically output the transmission. Only json content is displayed in your code, but curl_exec($cURL) returns 1 or 0 if something is wrong, and not the json content. Therefore, if you cannot get an array or object using json_decode , the JSON string is not in the $result variable.

To get what you want, you need to set another cURL option:

 curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1); 

Thus, curl_exec($cURL) will return the transfer as a string and will no longer output it automatically.

See the PHP manual for curl_exec return values.

Then you 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]); } 
+1
source

this can 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 />'; 
+1
source

All Articles