retrieving Google search results

旧巷老猫 提交于 2019-12-22 10:38:48

问题


i read your post on

simple php script to retrieve google keyword search completion

and i was wondering how would you go 'echo' the next page? here's my script..

$search = 'query';

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

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

i was able to 'echo' out the url, i am stucked in going to the next page and 'echo' out the next url's

thanks sir


回答1:


You change the index:

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

To loop through all:

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

You can inspect the complete result with var_dump($x);.

To retrieve another page of results, you can use the start parameter, e.g.:

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

You can request 8 results instead of 4 with rsz=large.




回答2:


For anyone else looking to interface with Google and stumbled on this solution, the code above now returns this response: 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) }

Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/) I hope this saves someone some time!



来源:https://stackoverflow.com/questions/3436525/retrieving-google-search-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!