Search code examples
phpfile-get-contentsgoogle-books

File Contents read in PHP


I can obtain information form google books in PHP using the following code

 $page1 = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:1933372826")
 $data = json_decode($page1, true);

I have an array of 1,200+ ISBNs so I want to be able to get this files so I can read it

So for example if I get each element of the array that is ISBN to be $book_isbn I tried the following but I get an error

 $page2 = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:$book_isbn")
 $book_data = json_decode($page2, true);

What should I change? Can I have $book_isbn in the file_get_contents?

This is the error I get

 Parse error: syntax error, unexpected '$book_data' (T_VARIABLE)

Solution

  • You can use

    $book_isbn = '1933372826';
    $page2 = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=isbn:$book_isbn");
    $book_data = json_decode($page2, true);
    echo '<pre>';print_r($book_data);
    

    If you have multiple value of book_isbn then use foreach and iterate over each element.