用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 相关文章推荐
MySQL授权问题总结
May 06 PHP
php木马webshell扫描器代码
Jan 25 PHP
php调用google接口生成二维码示例
Apr 28 PHP
php_screw安装使用教程(另一个PHP代码加密实现)
May 29 PHP
php实现计数器方法小结
Jan 05 PHP
深入php内核之php in array
Nov 10 PHP
Yii2简单实现多语言配置的方法
Jul 23 PHP
PHP中empty,isset,is_null用法和区别
Feb 19 PHP
phpStudy 2016 使用教程详解(支持PHP7)
Oct 18 PHP
PHP聚合式迭代器接口IteratorAggregate用法分析
Dec 28 PHP
定位地理位置PHP判断员工打卡签到经纬度是否在打卡之内
May 23 PHP
php 中self,this的区别和操作方法实例分析
Nov 04 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 shell命令合并图片的代码
2011/06/23 PHP
php 数据库字段复用的基本原理与示例
2011/07/22 PHP
PHP大神的十大优良习惯
2016/09/14 PHP
PHP利用百度ai实现文本和图片审核
2019/05/08 PHP
使用EXT实现无刷新动态调用股票信息
2008/11/01 Javascript
JQuery 常用操作代码
2010/03/14 Javascript
用apply让javascript函数仅执行一次的代码
2010/06/27 Javascript
jquery用get实现ajax在ie里面刷新不进入后台解决方法
2013/08/12 Javascript
JSuggest自动匹配下拉框使用方法(示例代码)
2013/12/27 Javascript
jQuery中ajax错误调试分析
2016/12/01 Javascript
详解angular element()方法使用
2017/04/08 Javascript
详解vue-cli 脚手架项目-package.json
2017/07/04 Javascript
微信小程序实现人脸识别
2018/05/25 Javascript
vue 路由懒加载中给 Webpack Chunks 命名的方法
2020/04/24 Javascript
[01:00:44]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第三局
2016/03/04 DOTA
python测试驱动开发实例
2014/10/08 Python
初步认识Python中的列表与位运算符
2015/10/12 Python
浅谈pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
2018/04/10 Python
Python 使用 docopt 解析json参数文件过程讲解
2019/08/13 Python
Python将视频或者动态图gif逐帧保存为图片的方法
2019/09/10 Python
实例讲解Python 迭代器与生成器
2020/07/08 Python
印度化妆品购物网站:Nykaa
2018/07/22 全球购物
Why do we need Unit test
2013/01/03 面试题
升职自荐书范文
2013/11/28 职场文书
专业幼师实习生自我鉴定范文
2013/12/08 职场文书
小学班干部竞选演讲稿
2014/04/24 职场文书
服务口号大全
2014/06/11 职场文书
厨师长岗位职责范本
2014/08/25 职场文书
给下属加薪申请报告
2015/05/15 职场文书
Nginx安装完成没有生成sbin目录的解决方法
2021/03/31 Servers
Django如何创作一个简单的最小程序
2021/05/12 Python
使用tensorflow 实现反向传播求导
2021/05/26 Python
java基础——多线程
2021/07/03 Java/Android
MongoDB连接数据库并创建数据等使用方法
2021/11/27 MongoDB
MySQL如何使备份得数据保持一致
2022/05/02 MySQL