9

初めて、GoogleブックスAPIからのデータを必要とするPHPアプリケーションを実装しています。かなり厄介ですが、一般的なクエリ 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個だけです。

対応するドキュメントを読んだ後、私は解決策を結論付けていません。

誰か助けてもらえますか?

4

3 に答える 3

14

今日、キーワードはraina77owの答えのようではmaxResultsありません。ソース。ドキュメントに記載されているように、一度に取得できる書籍の最大数は40です。ただし、次のPHP関数のように、複数のリクエストでこの制限を克服できます。max-results

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冊の本を返します。うまくいけば、誰かがこれを使用するでしょう、頑張ってください!

于 2012-11-29T16:12:39.397 に答える
13

いいえ、別のパラメータ:max-results。しかし、1つの「ページ」の結果の最大数はまだかなり低く設定されているようです(20)。

たとえば、次のクエリは次のとおりです。

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

...私に20のエントリを与えました。start-indexただし、次のように、paramを使用してコレクションを反復処理できると便利です。

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

...次に41、61など。0ではなく1で始まり、確認しました。)。

于 2012-07-07T14:42:53.457 に答える
1

これが、 maxResultsパラメーターを使用して20を超える結果を取得するために達成した方法です。

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

リクエストごとに40件の結果が返されます。以下のスクリーンショットを参照してください。

ここに画像の説明を入力してください

于 2020-08-25T13:56:59.777 に答える