0

I have written a PHP code to parse the data pobidd by GoogleBooks API

<?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"];

?>

When execute it, I get

Notice: Undefined index: volumeInfo in /storage/ssd3/164/2474164/public_html/dev/fetch/v2.php on line 8

for all the echo strings , so I re-checked all possible sources of problem without solution

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46

2 Answers2

0

You're accessing the array elements in the wrong way. Do var_dump($obj); or echo '<pre>'; print_r($obj); echo '</pre>'; to see the complete array structure. Your echo statements would be like this:

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 />';

Not sure whether you would be getting subtitle information along with the book details. If so, then please uncomment that line.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
0

To my mind it is far easier to use the object notation for accessing the various properties of the JSON response rather than the clunky array syntax but you need to identify the correct location in the object/array heirarchy before attempting to access the sub-keys of it.

$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>';

Will output

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.
                        )

                )

        )

)
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46