用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之第四天
Oct 09 PHP
php 生成唯一id的几种解决方法
Mar 08 PHP
PHP的构造方法,析构方法和this关键字详细介绍
Oct 22 PHP
获取URL文件名后缀
Oct 24 PHP
PHP+Mysql+Ajax+JS实现省市区三级联动
May 23 PHP
PHP实现的博客欢迎提示功能(很特别哦)
Jun 05 PHP
PHP中使用imagick生成PSD文件缩略图教程
Jan 26 PHP
php动态添加url查询参数的方法
Apr 14 PHP
PHP查找与搜索数组元素方法总结
Jun 12 PHP
yii2超好用的日期组件和时间组件
May 05 PHP
yii2利用自带UploadedFile实现上传图片的示例
Feb 16 PHP
PHP实现普通hash分布式算法简单示例
Aug 06 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
咖啡与水的关系
2021/03/03 冲泡冲煮
同时提取多条新闻中的文本一例
2006/10/09 PHP
php &amp;&amp; 逻辑与运算符使用说明
2010/03/04 PHP
PHP遍历二维数组的代码
2011/04/22 PHP
PHP自动生成后台导航网址的最佳方法
2013/08/27 PHP
laravel框架使用阿里云短信发送消息操作示例
2020/02/15 PHP
javascript 一个自定义长度的文本自动换行的函数
2007/08/19 Javascript
jQuery 名称冲突的解决方法
2011/04/08 Javascript
扩展IE中一些不兼容的方法如contains、startWith等等
2014/01/09 Javascript
JavaScript使用indexOf获得子字符串在字符串中位置的方法
2015/04/06 Javascript
Javascript验证方法大全
2015/09/21 Javascript
jQuery+Ajax+PHP弹出层异步登录效果(附源码下载)
2016/05/27 Javascript
浅谈javascript基础之客户端事件驱动
2016/06/10 Javascript
jQuery对table表格进行增删改查
2020/12/22 Javascript
nodejs开发——express路由与中间件
2017/03/24 NodeJs
关于TypeScript模块导入的那些事
2018/06/12 Javascript
微信小程序实现左滑动删除效果
2020/03/30 Javascript
你不知道的 TypeScript 高级类型(小结)
2020/08/28 Javascript
vue界面发送表情的实现代码
2020/09/11 Javascript
[00:52]DOTA2国际邀请赛
2020/02/21 DOTA
python for 循环获取index索引的方法
2019/02/01 Python
基于python的docx模块处理word和WPS的docx格式文件方式
2020/02/13 Python
python 代码实现k-means聚类分析的思路(不使用现成聚类库)
2020/06/01 Python
基于Python爬取51cto博客页面信息过程解析
2020/08/25 Python
HTML5验证以及日期显示的实现详解
2013/07/05 HTML / CSS
HTML5 Canvas基本线条绘制的实例教程
2016/03/17 HTML / CSS
特步官方商城:Xtep
2017/03/21 全球购物
波兰购物网站:MALL.PL
2019/05/01 全球购物
毕业研究生的自我鉴定
2013/11/30 职场文书
读书心得体会
2013/12/28 职场文书
便利店投资创业计划书
2014/02/08 职场文书
给朋友的赠语
2015/06/23 职场文书
会计做账心得体会
2016/01/22 职场文书
Python天气语音播报小助手
2021/09/25 Python
在Docker容器中部署SQL Server
2022/04/11 Servers
Android开发手册TextInputLayout样式使用示例
2022/06/10 Java/Android