我正在尝试从 Freebase API 获取所有旅行目的地。根据文档,我正在使用光标和限制参数。我从 0 处的光标开始并限制 100,然后在每次迭代中将 100 添加到光标。问题是我这样只能得到 76 个结果。Freebase 数据库中应该有 cca 1500 个旅游目的地。
文档:
https://developers.google.com/freebase/v1/search
这是我的代码:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$apiKey = 'SUPER_SECRET_API_KEY';
use Guzzle\Http\Client;
//// GOOGLE FREEBASE
$client = new Client('https://www.googleapis.com');
$cursor = 0;
$limit = 100;
$results = array();
do {
$request = $client->get('/freebase/v1/search');
$request->getQuery()->set('key', $apiKey);
$request->getQuery()->set('filter', '(all type:/travel/travel_destination)');
$request->getQuery()->set('cursor', $cursor);
$request->getQuery()->set('limit', $limit);
$request->getQuery()->set('indent', 'true');
$request->getQuery()->set('query', '');
$response = $request->send()->json();
foreach ($response['result'] as $result) {
$results[] = $result;
}
$cursor += $limit;
} while (count($response['result']) > 0);
echo count($results);exit;
它打印 76。