Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Books API returns only 10 results

I am implementing a PHP application that requires some data from Google Books API, for the first time. It is quite awkward, but when I use the typical query http://books.google.com/books/feeds/volumes?q=search+term (someone who has previous experience with the specific API, can understand me), it returns only 10 results.

For instance, the XML response for http://books.google.com/books/feeds/volumes?q=php, contains the following field: totalResults>582. However, I retrieve only 10.

After reading the corresponding documentation, I have not concluded into a solution.

Could anyone help me?

like image 210
thitami Avatar asked Jul 07 '12 12:07

thitami


3 Answers

Nowadays, the keyword is maxResults and not max-results as in raina77ow's answer. Source. As the documentation says, the maximum number of books that can be retrieved at once is 40. However, you can overcome this limitation with multiple requests, like with this PHP-function:

private $books = array('items' => array());

/**
 * Searches the Google Books database through their public API
 * and returns the result. Notice that this function will (due to
 * Google's restriction) perform one request per 40 books.
 * If there aren't as many books as requested, those found will be
 * returned. If no books at all is found, false will be returned.
 * 
 * @author Dakniel
 * @param string $query Search-query, API documentation
 * @param int $numBooksToGet Amount of results wanted
 * @param int [optional] $startIndex Which index to start searching from
 * @return False if no book is found, otherwise the books
 */
private function getBooks($query, $numBooksToGet, $startIndex = 0) {

    // If we've already fetched all the books needed, or
    // all results are already stored
    if(count($this->books['items']) >= $numBooksToGet)
        return $this->books;

    $booksNeeded = $numBooksToGet - count($this->books['items']);

    // Max books / fetch = 40, this is therefore our limit
    if($booksNeeded > 40)
        $booksNeeded = 40;

    $url = "https://www.googleapis.com/books/v1/volumes?q=". urlencode($query) ."&startIndex=$startIndex&maxResults=$booksNeeded";

    // Get the data with cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $bookBatch = curl_exec($ch);
    curl_close($ch);

    // If we got no matches..
    if($bookBatch['totalItems'] === 0) {

        // .. but we already have some books, return those
        if(count($this->books) > 0)
            return $this->books;
        else
            return false;

    }

    // Convert the JSON to an array and merge the existing books with
    // this request's new books 
    $bookBatch = json_decode($bookBatch, true);
    $this->books['items'] = array_merge($this->books['items'], $bookBatch['items']);

    // Even if we WANT more, but the API can't give us more: stop
    if( ($bookBatch['totalItems'] - count($this->books['items'])) === 0 ) {
        return $this->books;
    }

    // We need more books, and there's more to get: use recursion
    return $this->getBooks($query, $numBooksToGet, $startIndex);

}

Simple example usage:

$books = $this->getBooks("programming", 50);

Which will return up to 50 books which matches the keyword programming. Hopefully someone will have usage of this, good luck!

like image 84
Zar Avatar answered Oct 07 '22 11:10

Zar


No, its another param: max-results. But it looks like the maximum number of results in one 'page' is still set quite low - at 20.

For example, this query:

http://books.google.com/books/feeds/volumes?q=php&max-results=40

... gave me just 20 entries. It's useful, though, that you can iterate over the collection with start-index param, like this:

http://books.google.com/books/feeds/volumes?q=php&max-results=20&start-index=21

... then 41, 61 etc. It starts with 1, and not 0, I've checked. )

Edit 2022

The param is now maxResults not max-results and startIndex not start-index

like image 20
raina77ow Avatar answered Oct 07 '22 09:10

raina77ow


This is how I achieved to get more than 20 results using the maxResults parameter

'https://www.googleapis.com/books/v1/volumes?q=inauthor:danielle%20steele&maxResults=40&key=your_very_own_api_key'

It returns 40 results per request, breeze. See screenshot below.

enter image description here

like image 1
Iwan Ross Avatar answered Oct 07 '22 11:10

Iwan Ross