用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+mysql扎实个人基本功
Mar 27 PHP
php设计模式 Builder(建造者模式)
Jun 26 PHP
PHP中获取内网用户MAC地址(WINDOWS/linux)的实现代码
Aug 11 PHP
PHP服务器页面间跳转实现方法
Aug 02 PHP
利用curl 多线程 模拟 并发的详解
Jun 14 PHP
php将一维数组转换为每3个连续值组成的二维数组
May 06 PHP
php实现的一段简单概率相关代码
May 30 PHP
浅谈PHP正则中的捕获组与非捕获组
Jul 18 PHP
thinkPHP模板中for循环与switch语句用法示例
Nov 30 PHP
yii2-GridView在开发中常用的功能及技巧总结
Jan 07 PHP
PHP+jQuery实现滚屏无刷新动态加载数据功能详解
May 04 PHP
ThinkPHP 3.2.3实现页面静态化功能的方法详解
Aug 03 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 如何利用phpexcel导入数据库
2013/08/24 PHP
找到一点可怜的关于dojo资料,谢谢作者!
2006/12/06 Javascript
jQuery中文入门指南,翻译加实例,jQuery的起点教程
2007/01/13 Javascript
setAttribute 与 class冲突解决
2008/02/17 Javascript
js判断输入是否为正整数、浮点数等数字的函数代码
2010/11/17 Javascript
jQuery前台数据获取实现代码
2011/03/16 Javascript
javascript验证身份证完全方法具体实现
2013/11/18 Javascript
基于JQuery实现的图片自动进行缩放和裁剪处理
2014/01/31 Javascript
node.js中的buffer.toJSON方法使用说明
2014/12/14 Javascript
js中setTimeout()与clearTimeout()用法实例浅析
2015/05/12 Javascript
JavaScript中的small()方法使用详解
2015/06/08 Javascript
浅谈JS原生Ajax,GET和POST
2016/06/08 Javascript
JS中with的替代方法与String中的正则方法详解
2016/12/23 Javascript
ES6教程之for循环和Map,Set用法分析
2017/04/10 Javascript
vue实现简单表格组件实例详解
2017/04/16 Javascript
jQuery niceScroll滚动条错位问题的解决方法
2018/02/03 jQuery
js构建二叉树进行数值数组的去重与优化详解
2018/03/26 Javascript
基于JavaScript实现瀑布流布局
2018/08/15 Javascript
详解微信小程序input标签正则初体验
2018/08/18 Javascript
微信小程序显示倒计时功能示例【测试可用】
2018/12/03 Javascript
js微信分享接口调用详解
2019/07/23 Javascript
JavaScript语法约定和程序调试原理解析
2020/11/03 Javascript
Python通过RabbitMQ服务器实现交换机功能的实例教程
2016/06/29 Python
python素数筛选法浅析
2018/03/19 Python
python 中字典嵌套列表的方法
2018/07/03 Python
使用selenium模拟登录解决滑块验证问题的实现
2019/05/10 Python
Python实现 PS 图像调整中的亮度调整
2019/06/28 Python
在python plt图表中文字大小调节的方法
2019/07/08 Python
python网络编程之多线程同时接受和发送
2019/09/03 Python
tensorflow使用range_input_producer多线程读取数据实例
2020/01/20 Python
JD Sports马来西亚:英国领先的运动鞋和运动服饰零售商
2018/03/13 全球购物
什么是.net的Remoting技术
2016/07/08 面试题
企业厂务公开实施方案
2014/03/26 职场文书
买卖合同纠纷代理词
2015/05/25 职场文书
工作经历证明范本
2015/06/15 职场文书
Pandas-DataFrame知识点汇总
2022/03/16 Python