0

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 中有标题。我想得到title。所以我写了以下foreach循环

foreach($bookBatch['items'] as $row)
    {
    foreach($row['volumeInfo'] as $volumeinfo)
       {

echo $volumeinfo['title'];

       }    
    }

但我得到这样的输出

TP2TB0ehhhTS2YBpehhhTP2TB0ehhhTAN2TB0ehhhPIT1IB0ehhhP2SB0ehhhPP1TBpehhhPW1SBpehhhSTP2HB0ehhhPAP2IB0ehhh

相反,我应该得到像 The 48 Laws of Power 这样的标题

编辑 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);
4

1 回答 1

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']);
}
于 2013-10-15T17:08:48.923 回答