用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截取后台登陆密码的代码
May 05 PHP
采用header定义为文件然后readfile下载(隐藏下载地址)
Jan 31 PHP
php教程之phpize使用方法
Feb 12 PHP
php中socket的用法详解
Oct 24 PHP
Laravel框架实现发送短信验证功能代码
Jun 06 PHP
PHP中explode函数和split函数的区别小结
Aug 24 PHP
Laravel5.1自定义500错误页面示例
Oct 09 PHP
PHP实现根据数组的值进行分组的方法
Apr 20 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
Feb 11 PHP
php利用array_search与array_column实现二维数组查找
Jul 08 PHP
PHP实现简单登录界面
Oct 23 PHP
is_file和file_exists效率比较
Mar 14 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
编译问题
2006/10/09 PHP
apache rewrite_module模块使用教程
2008/01/10 PHP
php中jQuery插件autocomplate的简单使用笔记
2012/06/14 PHP
Laravel执行migrate命令提示:No such file or directory的解决方法
2016/03/16 PHP
Symfony2框架学习笔记之表单用法详解
2016/03/18 PHP
php中bind_param()函数用法分析
2017/03/28 PHP
php 自定义函数实现将数据 以excel 表格形式导出示例
2019/11/13 PHP
用 Javascript 验证表单(form)中多选框(checkbox)值
2009/09/08 Javascript
jQuery 位置函数offset,innerWidth,innerHeight,outerWidth,outerHeight,scrollTop,scrollLeft
2010/03/23 Javascript
Jquery调用webService远程访问出错的解决方法
2010/05/21 Javascript
js新闻滚动 js如何实现新闻滚动效果
2013/01/07 Javascript
验证控件与Button的OnClientClick事件详细解析
2013/12/04 Javascript
javascript函数定义的几种区别小结
2014/01/06 Javascript
JS中使用Array函数shift和pop创建可忽略参数的例子
2014/05/28 Javascript
jquery实现显示已选用户
2014/07/21 Javascript
轻松创建nodejs服务器(10):处理POST请求
2014/12/18 NodeJs
javascript中 try catch用法
2015/08/16 Javascript
JS中mouseup事件丢失的原因与解决办法
2017/06/14 Javascript
在Vue中使用highCharts绘制3d饼图的方法
2018/02/08 Javascript
jQuery实现的淡入淡出与滑入滑出效果示例
2018/04/18 jQuery
关于Vue组件库开发详析
2018/07/01 Javascript
vue 刷新之后 嵌套路由不变 重新渲染页面的方法
2018/09/13 Javascript
在Node.js中将SVG图像转换为PNG,JPEG,TIFF,WEBP和HEIF格式的方法
2019/08/22 Javascript
jQuery - AJAX load() 实例用法详解
2019/08/27 jQuery
[57:22]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第五场
2018/04/10 DOTA
Python装饰器(decorator)定义与用法详解
2018/02/09 Python
Python中遍历列表的方法总结
2019/06/27 Python
matlab中imadjust函数的作用及应用举例
2020/02/27 Python
印度首个本地在线平台:nearbuy
2019/03/28 全球购物
如何理解委托
2012/01/06 面试题
致100米运动员广播稿
2014/02/14 职场文书
机电专业大学生职业规划书范文
2014/02/25 职场文书
2014年寒假社会实践活动心得体会
2014/04/07 职场文书
建国大业电影观后感
2015/06/01 职场文书
Vue h函数的使用详解
2022/02/18 Vue.js
JS轻量级函数式编程实现XDM二
2022/06/16 Javascript