google api的响应是这样的
{
"kind": "books#volumes",
"totalItems": 1956,
"items": [
{
"kind": "books#volume",
"id": "P_zMW3EHnTEC",
"etag": "BWKRqUEh9VU",
"selfLink": "https://content.googleapis.com/books/v1/volumes/P_zMW3EHnTEC",
"volumeInfo": {
"title": "The 48 Laws Of Power",
"authors":
现在主数组是$ bookBatch,其中的项目是一个数组。 items数组包含volumeInfo数组。在那个volumeInfo中有标题。 我想获得冠军。所以我写了以下foreach循环
foreach($bookBatch['items'] as $row)
{
foreach($row['volumeInfo'] as $volumeinfo)
{
echo $volumeinfo['title'];
}
}
但我得到这样的输出
TP2TB0ehhhTS2YBpehhhTP2TB0ehhhTAN2TB0ehhhPIT1IB0ehhhP2SB0ehhhPP1TBpehhhPW1SBpehhhSTP2HB0ehhhPAP2IB0ehhh
相反,我应该获得像48权力法则这样的标题
编辑1
$query="power";
$url = "https://www.googleapis.com/books/v1/volumes?q=".$query;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$bookBatch = curl_exec($ch);
curl_close($ch);
$bookBatch = json_decode($bookBatch, true);
答案 0 :(得分:1)
假设您的数据如下所示
$json = '{
"kind": "books#volumes",
"items": [
{
"kind": "books#volume",
"id": "_ojXNuzgHRcC",
"etag": "OTD2tB19qn4",
"selfLink": "https://www.googleapis.com/books/v1/volumes/_ojXNuzgHRcC",
"volumeInfo": {
"title": "Flowers",
"authors": [
"Vijaya Khisty Bodach"
]
}}]}';
$data = json_decode($json, true);
foreach($data['items'] as $item)
{
echo $item['volumeInfo']['title'];
// This will show the Authors names
echo implode(', ', $item['volumeInfo']['authors']);
}