检索Google搜索结果

时间:2010-08-08 23:42:32

标签: php search

我在

上看了你的帖子

simple php script to retrieve google keyword search completion

我想知道你怎么会在下一页'回声'? 这是我的剧本..

  

$ search ='query';

     

$ x = json_decode(file_get_contents(   'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='   。 urlencode($ search)));

     

回波   $ X-> responseData->结果[0] - > URL;

我能够'回显'网址,我被迫进入下一页并“回应”下一个网址

谢谢先生

2 个答案:

答案 0 :(得分:5)

您更改了索引:

echo $x->responseData->results[1]->url;

循环遍历所有:

foreach ($x->responseData->results as $r) {
    echo $r->url, "\n";
}

您可以使用var_dump($x);检查完整的结果。

要检索另一页结果,您可以使用start参数,例如:

$x = json_decode(
    file_get_contents(
    'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=4&q='
    . urlencode( $search )));

您可以使用rsz=large请求8个结果,而不是4个。

答案 1 :(得分:1)

对于任何希望与Google进行交互并偶然发现此解决方案的人,上面的代码现在会返回此响应:object(stdClass)#1 (3) { ["responseData"]=> NULL ["responseDetails"]=> string(143) "The Google Web Search API is no longer available. Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/)" ["responseStatus"]=> int(403) }

请迁移到Google自定义搜索API(Wikipedia我希望这能节省一些时间!