用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+javascript模拟Matrix画面
Oct 09 PHP
PHP curl 抓取AJAX异步内容示例
Sep 09 PHP
php查找指定目录下指定大小文件的方法
Nov 28 PHP
使用PHPExcel操作Excel用法实例分析
Mar 26 PHP
php验证码的制作思路和实现方法
Nov 12 PHP
让你的PHP7更快之Hugepage用法分析
May 31 PHP
thinkPHP中验证码的简单实现方法
Dec 05 PHP
详解PHP函数 strip_tags 处理字符串缺陷bug
Jun 11 PHP
php+redis消息队列实现抢购功能
Feb 08 PHP
Codeigniter里的无刷新上传的实现代码
Apr 14 PHP
laravel框架模板之公共模板、继承、包含实现方法分析
Aug 30 PHP
PHP+redis实现的限制抢购防止商品超发功能详解
Sep 19 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
实用函数9
2007/11/08 PHP
兼容firefox,chrome的网页灰度效果
2011/08/08 PHP
php中让上传的文件大小在上传前就受限制的两种解决方法
2013/06/24 PHP
php获取数组元素中头一个数组元素值的实现方法
2014/12/20 PHP
详谈PHP面向对象中常用的关键字和魔术方法
2017/02/04 PHP
PHP/HTML混写的四种方式总结
2017/02/27 PHP
input 输入框获得/失去焦点时隐藏/显示文字(jquery版)
2013/04/02 Javascript
javascript loadScript异步加载脚本示例讲解
2013/11/14 Javascript
js模拟hashtable的简单实例
2014/03/06 Javascript
jQuery动态创建html元素的常用方法汇总
2014/09/05 Javascript
Jquery实现仿腾讯微博发表广播
2014/11/17 Javascript
Javascript基础教程之数据类型 (数值 Number)
2015/01/18 Javascript
jquery实现相册一下滑动两次的方法
2015/02/09 Javascript
nodejs事件的监听与触发的理解分析
2015/02/12 NodeJs
关于JavaScript中的this指向问题总结篇
2017/07/23 Javascript
js判断节假日实例代码
2017/12/27 Javascript
Vue-cli 移动端布局和动画使用详解
2020/08/10 Javascript
python str与repr的区别
2013/03/23 Python
使用Python制作获取网站目录的图形化程序
2015/05/04 Python
python MySQLdb Windows下安装教程及问题解决方法
2015/05/09 Python
python使用paramiko实现远程拷贝文件的方法
2016/04/18 Python
Python自定义函数定义,参数,调用代码解析
2017/12/27 Python
Python模拟随机游走图形效果示例
2018/02/06 Python
PyTorch CNN实战之MNIST手写数字识别示例
2018/05/29 Python
Python中pandas dataframe删除一行或一列:drop函数详解
2018/07/03 Python
在python中用url_for构造URL的方法
2019/07/25 Python
Python爬虫:Request Payload和Form Data的简单区别说明
2020/04/30 Python
英国最大的女士服装零售商:Bonmarché
2017/08/17 全球购物
实习生体会的自我评价范文
2013/11/28 职场文书
创业大赛策划书
2014/03/01 职场文书
法制宣传教育方案
2014/05/09 职场文书
政风行风评议工作总结
2014/10/21 职场文书
预备党员考察意见范文
2015/06/01 职场文书
2015年暑假生活总结
2015/07/13 职场文书
ThinkPHP5和ThinkPHP6的区别
2021/03/31 PHP
vue组件冲突之引用另一个组件出现组件不显示的问题
2022/04/13 Vue.js