【问题标题】:Google Books API returns only 10 resultsGoogle Books API 仅返回 10 个结果
【发布时间】:2012-07-07 16:16:30
【问题描述】:

我正在实现一个 PHP 应用程序,这是第一次需要来自 Google Books API 的一些数据。 这很尴尬,但是当我使用典型的查询时 http://books.google.com/books/feeds/volumes?q=search+term(有具体API经验的可以理解我),只返回10个结果。

例如,http://books.google.com/books/feeds/volumes?q=php 的 XML 响应包含以下字段:totalResults>582。但是,我只检索到 10 个。

看了相应的文档,我还没有得出解决办法。

谁能帮帮我?

【问题讨论】:

    标签: php xml api google-books


    【解决方案1】:

    如今,关键字是 maxResults 而不是 max-results,就像raina77ow 的回答一样。 Source。正如文档所说,一次可以检索的最大书籍数量是 40。但是,您可以通过多个请求来克服这个限制,例如使用这个 PHP 函数:

    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);
    
    }
    

    简单示例用法:

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

    这将返回最多 50 本与关键字编程相匹配的书籍。希望有人会使用这个,祝你好运!

    【讨论】:

      【解决方案2】:

      不,它的另一个参数:max-results。但看起来一个“页面”中的最大结果数仍然设置得很低 - 为 20。

      例如,这个查询:

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

      ... 只给了我 20 个条目。不过,您可以使用 start-index 参数遍历集合,这很有用,如下所示:

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

      ...然后是 41、61 等。它以 1 开头,而不是 0,我已经检查过了。 )

      【讨论】:

      • 刚试过。到目前为止,这似乎是最准确的答案,但我有点担心,谷歌没有预测或提供精确的文档,对于如此常见的事情。
      • 我希望(所有公司的)谷歌文档能够 100% 准确......遗憾的是,我已经在他们的文档中遇到了很多的不一致之处。此外,我认为这个 API 现在实际上已被弃用:我在寻找官方链接时发现的许多链接现在都已失效。
      【解决方案3】:

      这就是我使用 ma​​xResults 参数获得 20 多个结果的方法

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

      每个请求返回 40 个结果,轻而易举。请看下面的截图。

      【讨论】:

      • api_key必须输入吗?即使没有输入它也可以正常工作。期待您的建议
      猜你喜欢
      • 2014-01-14
      • 2011-05-29
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多