用PHP获取Google AJAX Search API 数据的代码
作者:bea
http://code.google.com/apis/ajaxsearch/documentation/#fonje 代码如下: // This example request includes an optional API key which you will need to // remove or replace with your own key. // Read more about why it's useful to have an API key
http://code.google.com/apis/ajaxsearch/documentation/#fonje
代码如下:
// This example request includes an optional API key which you will need to
// remove or replace with your own key.
// Read more about why it's useful to have an API key.
// The request also includes the userip parameter which provides the end
// user's IP address. Doing so will help distinguish this legitimate
// server-side traffic from traffic which doesn't come from an end-user.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "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 申请地址:
http://code.google.com/apis/ajaxsearch/signup.html
由此,我们可以写个函数像这样
代码如下:
function google_search_api($args, $referer = 'http:///', $endpoint = 'web'){
$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;
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(
'q' => '21andy.com', // 查询内容
'key' => '你申请到的API KEY',
'userip' => '你的IP地址',
));
header('Content-type: text/html; charset=utf-8;');
echo '<xmp>';
print_r($rez);
echo '</xmp>';
有用 | 无用
代码如下:
// This example request includes an optional API key which you will need to
// remove or replace with your own key.
// Read more about why it's useful to have an API key.
// The request also includes the userip parameter which provides the end
// user's IP address. Doing so will help distinguish this legitimate
// server-side traffic from traffic which doesn't come from an end-user.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "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 申请地址:
http://code.google.com/apis/ajaxsearch/signup.html
由此,我们可以写个函数像这样
代码如下:
function google_search_api($args, $referer = 'http:///', $endpoint = 'web'){
$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;
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(
'q' => '21andy.com', // 查询内容
'key' => '你申请到的API KEY',
'userip' => '你的IP地址',
));
header('Content-type: text/html; charset=utf-8;');
echo '<xmp>';
print_r($rez);
echo '</xmp>';
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- PHP中10个不常见却非常有用的函数
- 初次接触php抽象工厂模式(Elgg)
- PHP5与MySQL数据库操作常用代码 收集
- ajax+php打造进度条 readyState各状态
- elgg 获取文件图标地址的方法
- PHP 解决utf-8和gb2312编码转换问题
- ecshop 订单确认中显示省市地址信息的方法
- php smarty函数扩展
- php Smarty date_format [格式化时间日期]
- libmysql.dll与php.ini是否真的要拷贝到c:windows目录下呢
- php下获取客户端ip地址的函数
- PHP 中文简繁互转代码 完美支持大陆、香港、台湾及新加坡
- PHP 模拟$_PUT实现代码
- php Xdebug 调试扩展的安装与使用.
- php5 non-thread-safe和thread-safe这两个版本的区别分析
- php 无法载入mysql扩展
- PHP生成Flash动画的实现代码
- PHP 引用是个坏习惯
- PHP 页面编码声明方法详解(header或meta)