小编ben*_*ton的帖子

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

请原谅我是否已经回答.我已经看到关于json数据和openlibrary的各种答案

到目前为止,我从openlibrary获得的json数据和我在示例中看到的json数据似乎在格式上有所不同

我的问题是,使用php(或javascript)如何将数据导入数组或个体变量并将它们放入mysql数据库.

  • 除了上一个问题 - 我想在下面显示原始数据:

标题:书籍作者:书籍作者Isbn:Isbn数字等

然后将这些细节放入mysql数据库中

[更新2015-011-07]现在我收到了答案,我已经更新了下面的代码,以显示它应该如何.以下将从openlibrary请求json数据,它将作为字符串返回.$ 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);
?>
Run Code Online (Sandbox Code Playgroud)

加载页面时,将显示以下内容:

ISBN: 0789721813 title: Red Hat Linux author: Hellums, Duane
Run Code Online (Sandbox Code Playgroud)

javascript php mysql json

5
推荐指数
1
解决办法
1355
查看次数

标签 统计

javascript ×1

json ×1

mysql ×1

php ×1