0

I'm having trouble using the Google Books API

I am inexperienced in working with JSON.

My form:

<form action="action.php" method="POST">
<div class="form-group">
    <div class="campos">
        <label>
         Search
        </label>
        <input type="text" name="search"  style="margin-right: 10px; width:250px; float:left" class="input-field" placeholder="Title, Author..."  />    
        <button type="submit" id="search" class="btn btn-default">Search</button>
    </div>
</div>

My Action:

$var1 = "https://www.googleapis.com/books/v1/volumes?q=";
$var2 = urlencode($_POST['search']);
$str = str_replace(" ", "+", $var2);
$page = $var1.$str;
header ("location:$page");
$data = file_get_contents($page);
$data = json_decode($page, true);

So running this code is returned to me one json file containing something like:

{
 "kind": "books#volumes",
 "totalItems": 584,
 "items": [
  {
  "kind": "books#volume",
  "id": "NkHwo2hKW6YC",
  "etag": "FNozDEeE+zY",
  "selfLink": "https://www.googleapis.com/books/v1/volumes/NkHwo2hKW6YC",
  "volumeInfo": {
  "title": "viagem ao redor da lua",
  "authors": [
   "JULIO VERNE"
  ],
  "publisher": "Hemus",
  "description": "Description of book",
  "industryIdentifiers": [
   {
   "type": "ISBN_10",
   "identifier": "8528901882"
   },
   {
    "type": "ISBN_13",
    "identifier": "9788528901887"
   }
  ],
   "readingModes": {
   "text": false,
   "image": true
  },
  "pageCount": 192,
  "printType": "BOOK",
  "averageRating": 3.0,
  "ratingsCount": 4,
  "contentVersion": "1.1.1.0.preview.1",
  "imageLinks": {
  "smallThumbnail": "http://bks6.books.google.com.br/books?id=NkHwo2hKW6YC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
   "thumbnail": "http://bks6.books.google.com.br/books?id=NkHwo2hKW6YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
    },
  "language": "pt",
  "previewLink": "http://books.google.com.br/books?id=NkHwo2hKW6YC&printsec=frontcover&dq=Julio+verne&hl=&cd=1&source=gbs_api",
  "infoLink": "http://books.google.com.br/books?id=NkHwo2hKW6YC&dq=Julio+verne&hl=&source=gbs_api",
  "canonicalVolumeLink": "http://books.google.com.br/books/about/viagem_ao_redor_da_lua.html?hl=&id=NkHwo2hKW6YC"
 },
 "saleInfo": {
  "country": "BR",
  "saleability": "NOT_FOR_SALE",
  "isEbook": false
 },
 "accessInfo": {
  "country": "BR",
  "viewability": "PARTIAL",
  "embeddable": true,
  "publicDomain": false,
  "textToSpeechPermission": "ALLOWED",
  "epub": {
    "isAvailable": false
  },
  "pdf": {
   "isAvailable": false
  },
  "webReaderLink": "http://books.google.com.br/books/reader?id=NkHwo2hKW6YC&hl=&printsec=frontcover&output=reader&source=gbs_api",
  "accessViewStatus": "SAMPLE",
  "quoteSharingAllowed": false
 },
  "searchInfo": {
  "textSnippet": "Em uma época que não se conhecia a tecnologia, Verne consegue prever em forma de aventura, uma viagem pelo espaço até a Lua."
 }
 },
 ]  
}

I need to show some of this information by php.

2 Answers2

1

Well you ale retrieving the JSON info so that's the hard part.

In order to print the data via php try something like the following;

$page = file_get_contents($page);

$data = json_decode($page, true);

echo "Title = " . $data['items'][0]['volumeInfo']['title'];
echo "Authors = " . @implode(",", $data['items'][0]['volumeInfo']['authors']);    
echo "Pagecount = " . $data['items'][0]['volumeInfo']['pageCount'];
jonboy
  • 2,729
  • 6
  • 37
  • 77
-1

Take a look at the Google Books API client library for PHP https://developers.google.com/books/docs/v1/libraries (They have some examples.) What you're trying to do is not going to work for at least two reasons:

  1. You have to authenticate to the API with your API Key.
  2. header("Location: $page"); redirects; it doesn't grab the page contents.

Edit: The authenticating part is not true if you don't need to access or create your own "bookshelves." So you could try:

$data = file_get_contents($page);
$data = json_decode($data, true);
$item = $data->items;
developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • Hello, thanks for the feedback. In fact, I do not need to access the book shelf. I tried your solution and the return is file_get_contents(https://www.googleapis.com/books/v1/volumes?q=Julio verne): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\book1\action.php on line 9 where my line 9 contains $data = file_get_contents($page); – Marco Aurélio Aug 29 '14 at 00:32
  • @Marco Aurélio, Probably because of it being https. Check out this question http://stackoverflow.com/questions/1975461/file-get-contents-with-https – developerwjk Sep 02 '14 at 18:14
  • Hi @developerwjk, thanks for the response. I changed the code, but I'm still getting errors back. – Marco Aurélio Sep 02 '14 at 21:03