用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 多关键字 高亮显示实现代码
Apr 23 PHP
在PHP上显示JFreechart画的统计图方法
Nov 03 PHP
关于JSON以及JSON在PHP中的应用技巧
Nov 27 PHP
让codeigniter与swfupload整合的最佳解决方案
Jun 12 PHP
php实现比较两个文件夹异同的方法
Jun 18 PHP
使用纯php代码实现页面伪静态的方法
Jul 25 PHP
php自定义函数实现汉字转换utf8编码的方法
Sep 29 PHP
php微信公众平台开发(三)订阅事件处理
Dec 06 PHP
php输出图像的方法实例分析
Feb 16 PHP
yii2 resetful 授权验证详解
May 18 PHP
PHP迭代与递归实现无限级分类
Aug 28 PHP
解决laravel5中auth用户登录其他页面获取不到登录信息的问题
Oct 08 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
PHP中PDO事务处理操作示例
2018/05/02 PHP
PHP 7.4中使用预加载的方法详解
2019/07/08 PHP
在 PHP 和 Laravel 中使用 Traits的方法
2019/11/13 PHP
Ext.FormPanel 提交和 Ext.Ajax.request 异步提交函数的区别
2009/11/12 Javascript
jQuery的实现原理的模拟代码 -3 事件处理
2010/08/03 Javascript
Js 时间间隔计算的函数(间隔天数)
2011/11/15 Javascript
用最通俗易懂的代码帮助新手理解javascript闭包 推荐
2012/03/01 Javascript
jQuery焦点图切换特效插件封装实例
2013/08/18 Javascript
JavaScript打印网页指定区域的例子
2014/05/03 Javascript
jQuery预加载图片常用方法
2015/06/15 Javascript
JS简单设置下拉选择框默认值的方法
2016/08/20 Javascript
在vue项目中引入高德地图及其UI组件的方法
2018/09/04 Javascript
vue自定义全局共用函数详解
2018/09/18 Javascript
nodejs中方法和模块用法示例
2018/12/24 NodeJs
JavaScript实现好看的跟随彩色气泡效果
2020/02/06 Javascript
element中的$confirm的使用
2020/04/26 Javascript
vue 图片裁剪上传组件的实现
2020/11/12 Javascript
[02:11]2014DOTA2 TI专访VG战队Fenrir:队伍气氛良好
2014/07/11 DOTA
在Python中使用SQLite的简单教程
2015/04/29 Python
Python中文件的读取和写入操作
2018/04/27 Python
Python 50行爬虫抓取并处理图灵书目过程详解
2019/09/20 Python
Django中自定义查询对象的具体使用
2019/10/13 Python
Python如何读写二进制数组数据
2020/08/01 Python
Bodum官网:咖啡和茶壶、玻璃器皿、厨房电器等
2018/08/01 全球购物
匡威德国官网:Converse德国
2019/01/26 全球购物
实习生自荐信范文
2013/11/13 职场文书
应聘会计求职信
2014/06/11 职场文书
邓小平理论心得体会
2014/09/09 职场文书
出差报告怎么写
2014/11/06 职场文书
《普罗米修斯》教学反思
2016/02/22 职场文书
2019安全宣传标语大全
2019/08/14 职场文书
Redis5之后版本的高可用集群搭建的实现
2021/04/27 Redis
python编写五子棋游戏
2021/05/25 Python
一文彻底理解js原生语法prototype,__proto__和constructor
2021/10/24 Javascript
TS 类型兼容教程示例详解
2022/09/23 Javascript