2017-09-03 28 views
0

我已經GoogleBooks APIPHP - 解析古爾書籍PI JSON

<?php 

    $isbn = "9781451648546"; // Steve Jobs book 
    $json = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=isbn:'.$isbn); 
    $obj = json_decode($json, true); 


    echo $obj["volumeInfo"]["title"]; 
    echo $obj["volumeInfo"]["title"]; 
    echo $obj["volumeInfo"]["subtitle"]; 
    echo $obj["volumeInfo"]["authors"]; 
    echo $obj["volumeInfo"]["printType"]; 
    echo $obj["volumeInfo"]["pageCount"]; 
    echo $obj["volumeInfo"]["publisher"]; 
    echo $obj["volumeInfo"]["publishedDate"]; 
    echo $obj["accessInfo"]["webReaderLink"]; 

?> 

寫了PHP代碼來解析數據pobidd當執行它,我得到

注意:未定義指數:volumeInfo在/存儲/ SSD3 /2474164分之164/的public_html的/ dev /獲取/ v2.php第8行

對所有回波字符串,所以我重新檢查PROBL的所有可能的源em沒有解決方案

+0

如果你只是'的print_r($ OBJ)'你會看到什麼在它... –

回答

0

您正在以錯誤的方式訪問數組元素。請參閱var_dump($obj);echo '<pre>'; print_r($obj); echo '</pre>';以查看完整的陣列結構。你echo陳述會是這樣的:

echo $obj['items'][0]["volumeInfo"]["title"] . '<br />'; 
// echo $obj['items'][0]["volumeInfo"]["subtitle"]; 
echo $obj['items'][0]["volumeInfo"]["authors"][0] . '<br />'; 
echo $obj['items'][0]["volumeInfo"]["printType"] . '<br />'; 
echo $obj['items'][0]["volumeInfo"]["pageCount"] . '<br />'; 
echo $obj['items'][0]["volumeInfo"]["publisher"] . '<br />'; 
echo $obj['items'][0]["volumeInfo"]["publishedDate"] . '<br />'; 
echo $obj['items'][0]["accessInfo"]["webReaderLink"] . '<br />'; 

不知道你是否會用書細節一起越來越字幕信息。如果是這樣,那麼請取消一行的註釋。

0

在我看來,使用對象表示法訪問JSON響應的各種屬性(而不是笨重的數組語法)要容易得多,但您需要在嘗試訪問對象/數組對象之前識別對象/數組對象的正確位置它的子鍵。

$isbn = "9781451648546"; // Steve Jobs book 
$json = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=isbn:'.$isbn); 
$obj = json_decode($json); 

$items=$obj->items; 
foreach($items as $item){ 
    /* Main keys */ 
    $vol=$item->volumeInfo; 
    $sales=$item->saleInfo; 
    $access=$item->accessInfo; 
    $info=$item->searchInfo; 

    /* subkeys */ 
    $title=$vol->title; 
    $authors=implode($vol->authors); 
    $type=$vol->printType; 
    /* etc */ 

    echo $title, $authors, $type, '<br />';//etc 
} 
/* to clearly see the data structure try this: */ 
echo '<pre>',print_r($obj,true),'</pre>'; 

將輸出

Steve JobsWalter IsaacsonBOOK 

stdClass Object 
(
    [kind] => books#volumes 
    [totalItems] => 1 
    [items] => Array 
     (
      [0] => stdClass Object 
       (
        [kind] => books#volume 
        [id] => 8U2oAAAAQBAJ 
        [etag] => Cfd5hfOLjks 
        [selfLink] => https://www.googleapis.com/books/v1/volumes/8U2oAAAAQBAJ 
        [volumeInfo] => stdClass Object 
         (
          [title] => Steve Jobs 
          [authors] => Array 
           (
            [0] => Walter Isaacson 
           ) 

          [publisher] => Simon and Schuster 
          [publishedDate] => 2011 
          [description] => Draws on more than forty interviews with Steve Jobs, as well as interviews with family members, friends, competitors, and colleagues to offer a look at the co-founder and leading creative force behind the Apple computer company. 
          [industryIdentifiers] => Array 
           (
            [0] => stdClass Object 
             (
              [type] => ISBN_13 
              [identifier] => 9781451648546 
             ) 

            [1] => stdClass Object 
             (
              [type] => ISBN_10 
              [identifier] => 1451648545 
             ) 

           ) 

          [readingModes] => stdClass Object 
           (
            [text] => 
            [image] => 
           ) 

          [pageCount] => 630 
          [printType] => BOOK 
          [categories] => Array 
           (
            [0] => Biography & Autobiography 
           ) 

          [averageRating] => 4 
          [ratingsCount] => 3904 
          [maturityRating] => NOT_MATURE 
          [allowAnonLogging] => 
          [contentVersion] => 0.3.0.0.preview.0 
          [imageLinks] => stdClass Object 
           (
            [smallThumbnail] => http://books.google.com/books/content?id=8U2oAAAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api 
            [thumbnail] => http://books.google.com/books/content?id=8U2oAAAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api 
           ) 

          [language] => en 
          [previewLink] => http://books.google.co.uk/books?id=8U2oAAAAQBAJ&printsec=frontcover&dq=isbn:9781451648546&hl=&cd=1&source=gbs_api 
          [infoLink] => http://books.google.co.uk/books?id=8U2oAAAAQBAJ&dq=isbn:9781451648546&hl=&source=gbs_api 
          [canonicalVolumeLink] => https://books.google.com/books/about/Steve_Jobs.html?hl=&id=8U2oAAAAQBAJ 
         ) 

        [saleInfo] => stdClass Object 
         (
          [country] => GB 
          [saleability] => NOT_FOR_SALE 
          [isEbook] => 
         ) 

        [accessInfo] => stdClass Object 
         (
          [country] => GB 
          [viewability] => PARTIAL 
          [embeddable] => 1 
          [publicDomain] => 
          [textToSpeechPermission] => ALLOWED_FOR_ACCESSIBILITY 
          [epub] => stdClass Object 
           (
            [isAvailable] => 
           ) 

          [pdf] => stdClass Object 
           (
            [isAvailable] => 
           ) 

          [webReaderLink] => http://play.google.com/books/reader?id=8U2oAAAAQBAJ&hl=&printsec=frontcover&source=gbs_api 
          [accessViewStatus] => SAMPLE 
          [quoteSharingAllowed] => 
         ) 

        [searchInfo] => stdClass Object 
         (
          [textSnippet] => Draws on more than forty interviews with Steve Jobs, as well as interviews with family members, friends, competitors, and colleagues to offer a look at the co-founder and leading creative force behind the Apple computer company. 
         ) 

       ) 

     ) 

)