2017-09-03 40 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. 
         ) 

       ) 

     ) 

)