PHP如何访问Google Search API

这篇文章给大家分享的是有关PHP如何访问Google Search API的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体如下:

这段代码段演示了如何从php向AJAX搜索API发送请求。请注意,此示例假定使用 PHP 5.2。对于较早安装的 PHP,请参考对应的官方注释。

具体代码如下:

$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton";
 
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.mysite.com/index.html");
$body = curl_exec($ch);
curl_close($ch);
 
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...

感谢各位的阅读!关于“PHP如何访问Google Search API”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!