用PHP获取Google AJAX Search API 数据的代码


Posted in PHP onMarch 12, 2010

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 = 'https://3water.com/', $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 相关文章推荐
php实现用户在线时间统计详解
Oct 08 PHP
支持中文的php加密解密类代码
Nov 27 PHP
使用PHP强制下载PDF文件示例
Jan 17 PHP
php异步多线程swoole用法实例
Nov 14 PHP
php实现递归与无限分类的方法
Feb 16 PHP
PHP实现QQ空间自动回复说说的方法
Dec 02 PHP
深入讲解PHP的Yii框架中的属性(Property)
Mar 18 PHP
详解EventDispatcher事件分发组件
Dec 25 PHP
基于php(Thinkphp)+jquery 实现ajax多选反选不选删除数据功能
Feb 24 PHP
ThinkPHP中create()方法自动验证实例
Apr 26 PHP
php读取本地json文件的实例
Mar 07 PHP
关于PHP中interface的用处详解
Jul 26 PHP
PHP开启gzip页面压缩实例代码
Mar 11 #PHP
php checkdate、getdate等日期时间函数操作详解
Mar 11 #PHP
PHP 5.3新特性命名空间规则解析及高级功能
Mar 11 #PHP
PHP Memcached + APC + 文件缓存封装实现代码
Mar 11 #PHP
了解Joomla 这款来自国外的php网站管理系统
Mar 11 #PHP
PHP调用Twitter的RSS的实现代码
Mar 10 #PHP
PHP中include()与require()的区别说明
Mar 10 #PHP
You might like
实用PHP会员权限控制实现原理分析
2011/05/29 PHP
jscript之Read an Excel Spreadsheet
2007/06/13 Javascript
javascript基础知识大集锦(二) 推荐收藏
2011/01/13 Javascript
javascript复制对象使用说明
2011/06/28 Javascript
使用PHP+JQuery+Ajax分页的实现
2013/04/23 Javascript
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
JavaScript基础语法、dom操作树及document对象
2014/12/02 Javascript
AngularJS 指令详细介绍
2016/07/27 Javascript
KnockoutJS 3.X API 第四章之数据控制流with绑定
2016/10/10 Javascript
学习vue.js条件渲染
2016/12/03 Javascript
nodejs+mongodb+vue前后台配置ueditor的示例代码
2018/01/02 NodeJs
JavaScript图片处理与合成总结
2018/03/04 Javascript
vuejs 动态添加input框的实例讲解
2018/08/24 Javascript
Vuex中的State使用介绍
2019/01/19 Javascript
Vue+iview+webpack ie浏览器兼容简单处理
2019/09/20 Javascript
Vue.js实现可编辑的表格
2019/12/11 Javascript
Vue作用域插槽实现方法及作用详解
2020/07/08 Javascript
Python读取sqlite数据库文件的方法分析
2017/08/07 Python
python编程羊车门问题代码示例
2017/10/25 Python
火车票抢票python代码公开揭秘!
2018/03/08 Python
python字符串与url编码的转换实例
2018/05/10 Python
pycharm 解除默认unittest模式的方法
2018/11/30 Python
线程安全及Python中的GIL原理分析
2019/10/29 Python
Python+numpy实现矩阵的行列扩展方式
2019/11/29 Python
Python自动化xpath实现自动抢票抢货
2020/09/19 Python
Expedia韩国官网:亚洲发展最快的在线旅游门户网站
2018/02/26 全球购物
日本快乐生活方式购物网站:Shop Japan
2018/07/17 全球购物
Speedo速比涛法国官方网站:泳衣、泳镜、泳帽、泳裤
2019/07/30 全球购物
学校教师读书活动总结
2014/07/08 职场文书
技术经济专业求职信
2014/09/03 职场文书
党委书记个人检查对照材料思想汇报
2014/10/11 职场文书
教师政风行风自查自纠报告
2014/10/21 职场文书
考试作弊检讨书
2014/10/21 职场文书
2016春季校长开学典礼致辞
2015/11/26 职场文书
2016机关干部作风建设心得体会
2016/01/21 职场文书
MySQL的意向共享锁、意向排它锁和死锁
2022/07/15 MySQL