用PHP获取Google AJAX Search API 数据的代码
2018-09-07 17:14
复制代码 代码如下:
// This example request includes an optional API key which you will need to
// remove or replace with your own key.
// Read more about why its useful to have an API key.
// The request also includes the userip parameter which provides the end
// users IP address. Doing so will help distinguish this legitimate
// server-side traffic from traffic which doesnt come from an end-user.
$url =
. q=Paris%20Hilton&key=INSERT-YOUR-KEY&userip=USERS-IP-ADDRESS;
// 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, /* Enter the URL of your site here */);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
API KEY 申请地址:
由此,我们可以写个函数像这样
复制代码 代码如下:
function google_search_api($args, $referer = //
$url =
if ( !array_key_exists(v, $args) )
$args[v] = 1.0;
$url .= ?.http_build_query($args, , &);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
return json_decode($body);
}
// 使用示例
$rez = google_search_api(array(
key => 你申请到的API KEY,
userip => 你的IP地址,
));
header(Content-type: text/html; charset=utf-8;);
echo <xmp>;
print_r($rez);
echo </xmp>;
文章标题:用PHP获取Google AJAX Search API 数据的代码
文章链接:http://soscw.com/essay/14558.html