用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 相关文章推荐
资料注册后发信小技巧
Oct 09 PHP
PHP读写文件的方法(生成HTML)
Nov 27 PHP
php+mysql分页代码详解
Mar 27 PHP
一步一步学习PHP(2)――PHP类型
Feb 15 PHP
PHP可逆加密/解密函数分享
Sep 25 PHP
php switch语句多个值匹配同一代码块应用示例
Jul 29 PHP
PHP实现的下载远程图片自定义函数分享
Jan 28 PHP
php计算两个日期相差天数的方法
Mar 14 PHP
PHP面向对象程序设计类的定义与用法简单示例
Dec 27 PHP
php mysql数据库操作类(实例讲解)
Aug 06 PHP
详解php用static方法的原因
Sep 12 PHP
laravel 实现设置时区的简单方法
Oct 10 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
ajax+php实现无刷新验证手机号的实例
2017/12/22 PHP
详解PHP 7.4 中数组延展操作符语法知识点
2019/07/19 PHP
自定义一个jquery插件[鼠标悬浮时候 出现说明label]
2011/06/27 Javascript
jQuery去掉字符串起始和结尾的空格(多种方法实现)
2013/04/01 Javascript
当鼠标移动时出现特效的JQuery代码
2013/11/08 Javascript
JS中的form.submit()不能提交表单的错误原因
2014/10/08 Javascript
jQuery实现鼠标点击弹出渐变层的方法
2015/07/09 Javascript
jQuery表格插件datatables用法汇总
2016/03/29 Javascript
jQuery实现的右下角广告窗体跟随效果示例
2016/09/16 Javascript
深入理解Vue 组件之间传值
2018/08/16 Javascript
详解如何配置vue-cli3.0的vue.config.js
2018/08/23 Javascript
vue中render函数的使用详解
2018/10/12 Javascript
深入学习TypeScript 、React、 Redux和Ant-Design的最佳实践
2019/06/17 Javascript
javascript设计模式 ? 解释器模式原理与用法实例分析
2020/04/17 Javascript
vue 使用localstorage实现面包屑的操作
2020/11/16 Javascript
Python的Twisted框架中使用Deferred对象来管理回调函数
2016/05/25 Python
基础的十进制按位运算总结与在Python中的计算示例
2016/06/28 Python
Python结巴中文分词工具使用过程中遇到的问题及解决方法
2017/04/15 Python
Python网络爬虫与信息提取(实例讲解)
2017/08/29 Python
在Python中执行系统命令的方法示例详解
2017/09/14 Python
python支付宝支付示例详解
2019/08/22 Python
Python selenium页面加载慢超时的解决方案
2020/03/18 Python
python快速安装OpenCV的步骤记录
2021/02/22 Python
CSS3中的display:grid,网格布局介绍
2019/10/30 HTML / CSS
国外最大的眼镜网站:Coastal
2017/08/09 全球购物
爱尔兰橄榄球店:Irish Rugby Store
2019/12/05 全球购物
Optimalprint加拿大:在线打印服务
2020/04/03 全球购物
.NET里面如何取得当前的屏幕分辨率
2012/12/06 面试题
车队司机自我鉴定
2014/03/02 职场文书
2015年大学生党员承诺书
2015/04/27 职场文书
毕业欢送会致辞
2015/07/29 职场文书
体育委员竞选稿
2015/11/21 职场文书
2019年大学推荐信
2019/06/24 职场文书
一文搞懂MySQL索引页结构
2022/02/28 MySQL
讲解MySQL增删改操作
2022/05/06 MySQL
springboot+rabbitmq实现智能家居实例详解
2022/07/23 Java/Android