用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添加文章时生成静态HTML文章的实现代码
Feb 17 PHP
通过PHP current函数获取未知字符键名数组第一个元素的值
Jun 24 PHP
CI(CodeIgniter)框架中的增删改查操作
Jun 10 PHP
PHP反射使用实例和PHP反射API的中文说明
Jul 02 PHP
Yii把CGridView文本框换成下拉框的方法
Dec 03 PHP
CodeIgniter集成smarty的方法详解
May 26 PHP
ThinkPHP3.2.1图片验证码实现方法
Aug 19 PHP
php array_slice 取出数组中的一段序列实例
Nov 04 PHP
深入了解PHP中的Array数组和foreach
Nov 06 PHP
php实现简单的权限管理的示例代码
Aug 25 PHP
360搜索引擎自动收录php改写方案
Apr 28 PHP
PHP实现打包zip并下载功能
Jun 12 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
程序员的表白神器“520”大声喊出来
2016/05/20 PHP
PHP常见的6个错误提示及解决方法
2016/07/07 PHP
Centos 6.5系统下编译安装PHP 7.0.13的方法
2016/12/19 PHP
javascript 学习之旅 (3)
2009/02/05 Javascript
js的写法基础分析
2011/01/17 Javascript
JQuery实现倒计时按钮具体方法
2013/11/14 Javascript
Knockout自定义绑定创建方法
2015/12/26 Javascript
正则表达式替换html元素属性的方法
2016/11/26 Javascript
重新认识vue之事件阻止冒泡的实现
2018/08/02 Javascript
vue自定义键盘信息、监听数据变化的方法示例【基于vm.$watch】
2019/03/16 Javascript
JavaScript运行机制实例分析
2020/04/11 Javascript
vue中使用带隐藏文本信息的图片、图片水印的方法
2020/04/24 Javascript
关于Django外键赋值问题详解
2017/08/13 Python
Python实现基本数据结构中栈的操作示例
2017/12/04 Python
Python数据分析之双色球中蓝红球分析统计示例
2018/02/03 Python
python中数据爬虫requests库使用方法详解
2018/02/11 Python
实例介绍Python中整型
2019/02/11 Python
Python中那些 Pythonic的写法详解
2019/07/02 Python
Python中的正则表达式与JSON数据交换格式
2019/07/03 Python
django中forms组件的使用与注意
2019/07/08 Python
Python matplotlib图例放在外侧保存时显示不完整问题解决
2020/07/28 Python
举例详解CSS3中的Transition
2015/07/15 HTML / CSS
什么是CSS3 HSLA色彩模式?HSLA模拟渐变色条
2016/04/26 HTML / CSS
美国学校校服,儿童和婴儿服装:Cookie’s Kids
2016/10/14 全球购物
英国网络托管和域名领导者:Web Hosting UK
2017/10/15 全球购物
Java中有几种方法可以实现一个线程?用什么关键字修饰同步方法?stop()和suspend()方法为何不推荐使用?
2015/08/04 面试题
枚举和一组预处理的#define有什么不同
2016/09/21 面试题
linux系统都有哪些运行级别
2012/04/15 面试题
英语专业毕业个人求职自荐信
2013/09/21 职场文书
矫正人员思想汇报
2014/01/08 职场文书
工作交流会欢迎词
2014/01/12 职场文书
三关爱志愿服务活动方案
2014/08/17 职场文书
化验室岗位职责
2015/02/14 职场文书
老公写给老婆的检讨书
2015/05/06 职场文书
SpringCloud的JPA连接PostgreSql的教程
2021/06/26 Java/Android
Python使用Beautiful Soup(BS4)库解析HTML和XML
2022/06/05 Python