Search book details with Google Books API in PHP

Asked

Viewed 1,870 times

6

I’m using the Google Books API to get the details of a book:

And to get the details of a book, by searching for the name, I’m using the following example provided by them, of this case the file simple-query.php inside the briefcase examples.

What happens is that I can only do the research by author of books by calling this function:

$results = $service->volumes->listVolumes('Catarina Coelho', $optParams);

How can I do the research by the name of the book? I can’t find any reference to doing the research by the name of the book.

As I couldn’t do it from the top, I found an example on the internet, but the search is done by the URL

https://www.googleapis.com/books/v1/volumes?q=Memoria+historia+da+provincia+de+Santa+Catharina

If I use this solution through the URL, how can I fetch the result from the source and be saved inside a variable/array?

3 answers

2


You can read the data by JSON and so manipulate it the way you want:

<?php
$page = file_get_contents("https://www.googleapis.com/books/v1/volumes?q=Memoria+historia+da+provincia+de+Santa+Catharina");

$data = json_decode($page, true);

echo "Title = " . $data['items'][0]['volumeInfo']['title'];
echo "<br/>Authors = " . @implode(",", $data['items'][0]['volumeInfo']['authors']);    
?>

Here’s an example to get the Title and Authors of the book.

1

In the section Performing Search, it is said that it is possible to specify the name of the book (or part of it) using the parameter intitle. For example:

http https://www.googleapis.com/books/v1/volumes?q=intitle:Cem+Anos+de+Solidao

Answer:

{
 "kind": "books#volumes",
 "totalItems": 17,
 "items": [
  {
   "kind": "books#volume",
   "id": "iPxztgAACAAJ",
   "etag": "2jx8ktgU3G8",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/iPxztgAACAAJ",
   "volumeInfo": {
    "title": "Cem anos de solidão",
    "authors": [
     "Gabriel García Márquez"
    ],
    "publishedDate": "1973",
    "industryIdentifiers": [
     {
      "type": "OTHER",
      "identifier": "OCLC:6522501"
     }
    ],
    "readingModes": {
     "text": false,
     "image": false
    },
    "pageCount": 364,
    "printType": "BOOK",
    "contentVersion": "preview-1.0.0",
    "language": "en",
    "previewLink": "http://books.google.com/books?id=iPxztgAACAAJ&dq=intitle:Cem+Anos+de+Solidao&hl=&cd=1&source=gbs_api",
    "infoLink": "http://books.google.com/books?id=iPxztgAACAAJ&dq=intitle:Cem+Anos+de+Solidao&hl=&source=gbs_api",
    "canonicalVolumeLink": "http://books.google.com/books/about/Cem_anos_de_solid%C3%A3o.html?hl=&id=iPxztgAACAAJ"
   },
   "saleInfo": {
    "country": "BR",
    "saleability": "NOT_FOR_SALE",
    "isEbook": false
   },
   "accessInfo": {
    "country": "BR",
    "viewability": "NO_PAGES",
    "embeddable": false,
    "publicDomain": false,
    "textToSpeechPermission": "ALLOWED",
    "epub": {
     "isAvailable": false
    },
    "pdf": {
     "isAvailable": false
    },
    "webReaderLink": "http://books.google.com/books/reader?id=iPxztgAACAAJ&hl=&printsec=frontcover&output=reader&source=gbs_api",
    "accessViewStatus": "NONE",
    "quoteSharingAllowed": false
   }
  }
 ]
}

0

Try to use this code:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    'Step 1:- google API url responsible for returning the book detail in JASON format
    Const GOOGLEAPIURL As String = "https://www.googleapis.com/books/v1/volumes?q=&#8221;"

    If txtISBN.Text <> String.Empty Then
        Dim requestURL As String

        'Step 2:- Reformed the URL to target particular ISBN number
        requestURL = GOOGLEAPIURL + txtISBN.Text() + "+isbn"

        'Step 3: created Http webrequest for URL
        Dim wr As HttpWebRequest = HttpWebRequest.Create(requestURL)
        'Step4 : get the response of web request in http web response object
        Dim resp As HttpWebResponse = wr.GetResponse()

        'Step 5: passes the response stream in stream reader
        Dim sreader As New StreamReader(resp.GetResponseStream())
        'Step 6: parsing the reader(which is in Jason format) using JASON.NET

        Dim rss = JObject.Parse(sreader.ReadToEnd())

        'Step 7: if object find the fetch the detail

        If rss Is Nothing = False AndAlso rss.Count > 0 Then
            lblBookName.Text = rss.Item("items")(0).Item("volumeInfo").Item("title").ToString()
            lblAuthor.Text = rss.Item("items")(0).Item("volumeInfo").Item("authors").ToString().Replace("[", "").Replace("]", "").ToString()
            lblPublisher.Text = rss.Item("items")(0).Item("volumeInfo").Item("publisher").ToString()
        Else
            lblMessage.Text = "Sorry ISBN Number not found, Please try different number."
        End If
    Else
        lblMessage.Text = "Please enter the ISBN number"
    End If
End Sub
  • 3

    The OP language is php, and this code suggested in VB.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.