用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 相关文章推荐
Ajax PHP简单入门教程代码
Apr 25 PHP
PHP 出现乱码和Sessions验证问题的解决方法!
Dec 06 PHP
ThinkPHP 防止表单重复提交的方法
Aug 08 PHP
php 获取本地IP代码
Jun 23 PHP
深入解析fsockopen与pfsockopen的区别
Jul 05 PHP
php计算几分钟前、几小时前、几天前的几个函数、类分享
Apr 09 PHP
简单的php新闻发布系统教程
May 09 PHP
php与flash as3 socket通信传送文件实现代码
Aug 16 PHP
php实现的css文件背景图片下载器代码
Nov 11 PHP
WordPress自定义时间显示格式
Mar 27 PHP
php在数据库抽象层简单使用PDO的方法
Nov 03 PHP
windows下的WAMP环境搭建图文教程(推荐)
Jul 27 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
PHPMyAdmin 快速配置方法
2009/05/11 PHP
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
2013/05/08 PHP
解析:使用php mongodb扩展时 需要注意的事项
2013/06/18 PHP
关于PHP堆栈与列队的学习
2013/06/21 PHP
浅析php中如何在有限的内存中读取大文件
2013/07/02 PHP
使用PHPCMS搭建wap手机网站
2015/09/20 PHP
Thinkphp框架开发移动端接口(2)
2016/08/18 PHP
PHP实现批量检测网站是否能够正常打开的方法
2016/08/23 PHP
PHP中关于php.ini参数优化详解
2020/02/28 PHP
Jquery+WebService 校验账号是否已被注册的代码
2010/07/12 Javascript
高性能Javascript笔记 数据的存储与访问性能优化
2012/08/02 Javascript
图片Slider 带左右按钮的js示例
2013/08/30 Javascript
在JavaScript的AngularJS库中进行单元测试的方法
2015/06/23 Javascript
javascript实现dom元素可拖动
2016/03/21 Javascript
JS实现的数字格式化功能示例
2017/02/10 Javascript
使用Browserify来实现CommonJS的浏览器加载方法
2017/05/14 Javascript
JavaScript实现QQ列表展开收缩扩展功能
2017/10/30 Javascript
微信小程序之swiper轮播图中的图片自适应高度的方法
2018/04/23 Javascript
详解无限滚动插件vue-infinite-scroll源码解析
2019/05/12 Javascript
深入浅析Vue中mixin和extend的区别和使用场景
2019/08/01 Javascript
jQuery实现验证用户登录
2019/12/10 jQuery
Python处理文本换行符实例代码
2018/02/03 Python
Python 在字符串中加入变量的实例讲解
2018/05/02 Python
数组保存为txt, npy, csv 文件, 数组遍历enumerate的方法
2018/07/09 Python
python实现定时发送邮件
2020/12/23 Python
利用python实现后端写网页(flask框架)
2021/02/28 Python
html5实现多图片预览上传及点击可拖拽控件
2018/03/15 HTML / CSS
shell变量的作用空间是什么
2013/08/17 面试题
大学本科生的个人自我评价
2013/12/09 职场文书
幼儿园亲子活动方案
2014/01/29 职场文书
乡镇办公室工作决心书
2014/03/11 职场文书
社团活动总结范文
2014/04/26 职场文书
预备党员2014年第四季度思想汇报范文
2014/10/25 职场文书
医院党的群众路线教育实践活动学习心得体会
2014/10/30 职场文书
2014年大班保育员工作总结
2014/12/02 职场文书
如何利用STAR法则制作留学文书?
2019/08/26 职场文书