用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 相关文章推荐
dedecms 制作模板中使用的全局标记图文教程
Mar 11 PHP
php日期转时间戳,指定日期转换成时间戳
Jul 17 PHP
浅谈web上存漏洞及原理分析、防范方法(安全文件上存方法)
Jun 29 PHP
关于PHP的curl开启问题探讨
Apr 08 PHP
php保存二进制原始数据为图片的程序代码
Oct 14 PHP
在Debian系统下配置LNMP的教程
Jul 09 PHP
php实现curl模拟ftp上传的方法
Jul 29 PHP
基于PHP代码实现中奖概率算法可用于刮刮卡、大转盘等抽奖算法
Dec 20 PHP
PHP实现的随机IP函数【国内IP段】
Jul 20 PHP
phpstudy2018升级MySQL5.5为5.7教程(图文)
Oct 24 PHP
用php定义一个数组最简单的方法
Oct 04 PHP
php双向队列实例讲解
Nov 17 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
php 防止单引号,双引号在接受页面转义
2008/07/10 PHP
php的数组与字符串的转换函数整理汇总
2013/07/18 PHP
PHP在线书签系统分享
2016/01/04 PHP
PHP的Yii框架中创建视图和渲染视图的方法详解
2016/03/29 PHP
Symfony2针对输入时间进行查询的方法分析
2017/06/28 PHP
tp5实现微信小程序多图片上传到服务器功能
2018/07/16 PHP
JavaScript入门教程(11) js事件处理
2009/01/31 Javascript
jquery与js函数冲突的两种解决方法
2013/09/09 Javascript
JQuery触发事件例如click
2013/09/11 Javascript
angularjs学习笔记之双向数据绑定
2015/09/26 Javascript
解析javascript图片懒加载与预加载的分析总结
2016/10/27 Javascript
浅谈jquery选择器 :first与:first-child的区别
2016/11/20 Javascript
原生ajax处理json格式数据的实例代码
2016/12/25 Javascript
Html5 js实现手风琴效果
2020/04/17 Javascript
微信小程序 点击控件后选中其它反选实例详解
2017/02/21 Javascript
Node.js中多进程模块Cluster的介绍与使用
2017/05/27 Javascript
React Native之prop-types进行属性确认详解
2017/12/19 Javascript
Node中使用ES6语法的基础教程
2018/01/05 Javascript
elementUI Vue 单个按钮显示和隐藏的变换功能(两种方法)
2018/09/04 Javascript
用vscode开发vue应用的方法步骤
2019/05/06 Javascript
JS计算斐波拉切代码实例
2019/09/12 Javascript
vue远程加载sfc组件思路详解
2019/12/25 Javascript
Vue通过getAction的finally来最大程度避免影响主数据呈现问题
2020/04/24 Javascript
python操作字典类型的常用方法(推荐)
2016/05/16 Python
Python SqlAlchemy动态添加数据表字段实例解析
2018/02/07 Python
Python实现爬取亚马逊数据并打印出Excel文件操作示例
2019/05/16 Python
Python通过队列来实现进程间通信的示例
2020/10/14 Python
python 三种方法实现对Excel表格的读写
2020/11/19 Python
美国精品家居用品网站:US-Mattress
2016/08/24 全球购物
百度软件工程师职位
2013/02/14 面试题
汽车驾驶求职信
2013/10/25 职场文书
求职信格式范本
2013/11/15 职场文书
企业管理毕业生求职信范文
2014/03/07 职场文书
奥林匹克运动会口号
2014/06/19 职场文书
教师批评与自我批评
2014/10/15 职场文书
实训报告范文大全
2014/11/04 职场文书