龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

Google PHP 访问 Google Search API

时间:2014-06-25 02:08来源:网络整理 作者:网络 点击:
分享到:
PHP 访问 Google Search API 以下代码段演示了如何从 php 向 AJAX 搜索 API 发送请求。请注意,此示例假定使用 PHP 5.2。对于较早安装的 PHP,请参考此注释。**php代码**[代码片段(14行)]

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

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...
精彩图集

赞助商链接