简体   繁体   English

我如何解析openlibrary api中的Json数据? (正确)

[英]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 我已经看到关于json数据和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 到目前为止,我从openlibrary获得的json数据和我在示例中看到的json数据似乎在格式上有所不同

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. 我的问题是,使用php(或javascript)如何将数据导入数组或个体变量并将它们放入mysql数据库。

  • 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. 标题:书籍作者:书籍作者Isbn:Isbn数字等

and then put these details into a mysql data base 然后将这些细节放入mysql数据库中

[Update 2015-011-07] Now I have recieved the answer, I have updated the code below to show how it should be. [更新2015-011-07]现在我收到了答案,我已经更新了下面的代码,以显示它应该如何。 The following will request json data from openlibrary and it will be returned as a string. 以下将从openlibrary请求json数据,它将作为字符串返回。 the ISBN number in $url is just for testing purposes, so by all means change it. $ url中的ISBN号仅用于测试目的,因此一定要更改它。

<?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

By default, cURL automatically output the transfer. 默认情况下, cURL自动输出传输。 Your code only displays the json content, but curl_exec($cURL) returns 1 or 0 if something gets wrong, and not the json content. 您的代码仅显示json内容,但如果出现错误, curl_exec($cURL)将返回1或0,而不是json内容。 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. 这就是为什么你无法使用json_decode获取所需的数组或对象,JSON字符串不在$result变量中。

To obtain what you want, you need to set an other cURL option: 要获得所需内容,您需要设置其他cURL选项:

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. 这样curl_exec($cURL)将以字符串形式返回传输,并且不再自动输出。

See the PHP manual about the returned values of curl_exec . 有关curl_exec的返回值,请参阅PHP手册

Then you only need to use json_decode : 然后你只需要使用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]);
}

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 />';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM