提问人:Dango 提问时间:12/10/2019 最后编辑:dennisgonDango 更新时间:12/10/2019 访问量:342
使用 PHP 解析 JSON API
Parsing JSON API with PHP
问:
我正在研究 BooksRun API,我无法使用 PHP foreach 显示从 API 返回的任何数据。我的代码如下。提前致谢!
<?php
//PARAMETERS
$url = 'https://booksrun.com/api/v3/price/buy/0134093410?key=0t8rfbno7qc4lmaav9yz';
$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);
curl_close($cURL);
//Json Data decoded on PHP object
$obj_data = json_decode($result);
echo '<div>' . 'start : '.$obj_data->result->status . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->offers->booksrun->new->price . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->marketplace->used->price . '</div>';
foreach ($obj_data->result as $book) {
//fetch object data
echo '<div>' . 'seller: '.$book->offers->booksrun->rent->price . '</div>';
echo '<div>' . 'date: '.$book->marketplace->used->price . '</div>';
}
?>
答:
1赞
Prabhjot Singh Kainth
12/10/2019
#1
只需进行以下更改:
echo '<div>' . 'seller: '.$obj_data->result->offers->booksrun->rent->{"35"}->price . '</div>';
echo '<div>' . 'date: '.$obj_data->result->offers->marketplace[0]->used->price . '</div>';
$obj_data->result->offers->marketplace
是一个数组,因此您可以循环访问它。
无需遍历结果对象,因为它中只有一个项目。
评论
0赞
Dango
12/10/2019
如果 JSON 数据集有时是单个结果,有时是数组,该怎么办?此外,在我看来,API 动态显示 {“35”},因此数字一直在变化......在我看来有点愚蠢,但他们一定是有原因的......
0赞
Prabhjot Singh Kainth
12/10/2019
如果数据集返回多个值,则必须使用 foreach 循环。另外,如果您正在寻找动态密钥解析,请参阅此 stackoverflow.com/questions/31285360/...
1赞
Lopes Wang
12/10/2019
#2
如果将
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
后
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
评论
1赞
Lopes Wang
12/10/2019
由于 API 是 HTTPS,因此此选项允许您跳过对等方证书的验证并获取$result。
0赞
Lopes Wang
12/10/2019
谢谢伙计。这是一个很好的建议。
1赞
dennisgon
12/10/2019
#3
我认为您需要更改代码,因为 JSON 中有一个数组 也许你可以改成这个代码
<?php
//PARAMETERS
$url = 'https://booksrun.com/api/v3/price/buy/0134093410?key=0t8rfbno7qc4lmaav9yz';
$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);
curl_close($cURL);
//Json Data decoded on PHP object
$obj_data = json_decode($result);
echo '<div>' . 'start : '.$obj_data->result->status . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->offers->booksrun->new->price . '</div>';
//echo '<div>' . 'date: '.$obj_data->result->marketplace->used->price . '</div>';
//fetch object data
foreach ($obj_data->result->offers->booksrun->rent as $rent) {
echo '<div>' . 'seller: '.$rent->price . '</div>';
}
foreach ($obj_data->result->offers->marketplace as $marketplace) {
echo '<div>' . 'date: '.$marketplace->used->price. '</div>';
}
?>
代码中的问题是你想打开一个对象,但实际上它是一个数组
评论
$obj_data
var_dump($obj_data)
result
"35"
"rent"
->rent->{"35"}->price