3

目前我正在使用以下代码:

$zipcode = '91762';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restauraunts+".$zipcode."&rsz=large");
curl_setopt($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec($ch);
$data = ob_get_contents();
ob_end_clean();
curl_close($ch);
$restauraunts_array = json_decode($data, true);

        foreach($restauraunts_array['responseData']['results'] as $key => $value) {
            $results[] = array(
                'title' => $value['titleNoFormatting'],
                'address' => $value['streetAddress'],
                'city' => $value['city'],
                'state' => $value['region'],
                'zipcode' => $zipcode,
                'phone' => $value['phoneNumbers'][0]['number'],
                'lat' => $value['lat'],
                'lng' => $value['lng']
            );
        }

但它只会返回 7 个结果。我正在寻找一种方法来获得更多。我查看了 API 代码,但没有找到任何方法来获取更多结果。可以做到吗?您能否指出如何获得多个结果的文档/实现?

答案:Mikey 能够提供我正在寻找的答案。这是我为获得 32 个结果所做的工作:

 $zipcode = '91762';
    $results = array()
    $counter = array(0,8,16,24);
    foreach($counter as $page) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restauraunts+".$zipcode."&rsz=large&start=".$page);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    ob_start();
    curl_exec($ch);
    $data = ob_get_contents();
    ob_end_clean();
    curl_close($ch);
    $restauraunts_array = json_decode($data, true);
    if(!empty($restauraunts_array['responseData']['results'])) {
            foreach($restauraunts_array['responseData']['results'] as $key => $value) {
                $results[] = array(
                    'title' => $value['titleNoFormatting'],
                    'address' => $value['streetAddress'],
                    'city' => $value['city'],
                    'state' => $value['region'],
                    'zipcode' => $zipcode,
                    'phone' => $value['phoneNumbers'][0]['number'],
                    'lat' => $value['lat'],
                    'lng' => $value['lng']
                );
            }
      }
      return $results;
4

2 回答 2

3

目前,在大多数情况下,您最多只能获得 64 个结果 - 跨越 8 个页面,每个页面有 8 个结果 - 您可以使用 Search API 检索这些结果。此规则的例外是本地和博客。Local 最多返回 4 页,共 8 个结果,共 32 个,Blog 只返回前 8 个。

来源:http ://groups.google.com/group/Google-AJAX-Search-API/browse_thread/thread/db6616286ce83ca0

于 2010-09-01T14:27:15.023 回答
0

这是不正确的 - 4 页,每页有 8 个结果,这绝对是您可以检索的最大值。

本地搜索最多 32 个结果- 常规搜索最多 64 个结果。

于 2011-01-11T10:15:11.147 回答