如果已经回答,请原谅我。我已经看到关于json数据和openlibrary的各种答案
到目前为止,我从openlibrary获得的json数据和我在示例中看到的json数据似乎在格式上有所不同
我的问题是,使用php(或javascript)如何将数据导入数组或个体变量并将它们放入mysql数据库。
标题:书籍的平铺 作者:书籍作者 Isbn:Isbn号码 等
然后将这些细节放入mysql数据库
[更新2015-011-07]现在我收到了答案,我已经更新了下面的代码,以显示它应该如何。 以下将从openlibrary请求json数据,它将作为字符串返回。 $ url中的ISBN号仅用于测试目的,因此一定要更改它。
A B
1 8
2 12
3 8
加载页面时会显示以下内容:
<?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);
?>
答案 0 :(得分:1)
默认情况下,cURL
会自动输出转移。您的代码仅显示json内容,但如果出现错误,curl_exec($cURL)
将返回1或0,而不是json内容。这就是您无法使用json_decode
获取所需数组或对象的原因,JSON字符串不在$result
变量中。
要获得所需内容,您需要设置其他cURL选项:
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
这样curl_exec($cURL)
会将转移作为字符串返回,并且不会再自动输出。
请参阅PHP manual了解curl_exec
的返回值。
然后您只需要使用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 :(得分:1)
这可能会有所帮助
y1+50