1 谷歌短网址API接口介绍
谷歌短网址(Google url shortener)页面是http://goo.gl/,官方API文档说明是:点击进入。同新浪短网址的API一样,谷歌短网址的API调用可以通过两种授权方式(Authentication)实现。正如文档中所提到的:Every request your application sends to the Google URL Shortener API needs to identify your application to Google. There are two ways to identify your application: using an OAuth 2.0 token (which also authorizes the request) and/or using the application’s API key.(你的应用向谷歌短网址API服务器所发送的每一个请求,都需要向Google提供合法证明。有如下两种渠道来识别你的应用:使用一个OAuth 2.0 token,或者使用应用的API KEY)。由于OAuth 2.0的认证过程相对麻烦,而且谷歌官方文档也说了“An API key is highly recommended”,所以这里就使用第二种API KEY来实现。
2 使用Google url shortener的API KEY来调用谷歌短网址API
Google url shortener的API KEY申请方式见《Google 开发者控制台Developers Console简单介绍以及API KEY的生成》。在获得了API KEY后就可以使用下面的代码来实现谷歌短网址的API接口调用了:
<?php /** * @author: vfhky 201403012 20:31 * @description: PHP调用谷歌短网址API接口 * @reference: http://goo.gl/Ro277L * @param string $type: 非零整数代表长网址转短网址,0表示短网址转长网址 * @note: 长网址转短网址采用POST模式,短网址转长网址采用Get模式 */ function ggUrlAPI($type,$url){ if($type){ /* The API key is safe for embedding in URLs; it doesn't need any encoding. */ $key = 'AIzaSyBlRWs2M5c6a04l-YSDUpUKtlsvDK1hL1Q';//这是我申请的KEY,大家可以测试用 $data = array('longUrl' => $url, 'key' => $key); /* requests containing JSON content bodies must be accompanied by a Content-Type: application/json request header */ $post = json_encode($data); $baseurl = 'https://www.googleapis.com/urlshortener/v1/url'; } else $baseurl = 'https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$url; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $baseurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HEADER, 0); if($type){ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } $response = curl_exec($ch); $arrResponse = json_decode($response); curl_close($ch); return $arrResponse; } echo '谷歌短网址API接口测试结果: <br/> '; echo 'Long to Short: '; print_r(ggUrlAPI(1,'http://www.huangkeye.cn')); echo '<br/><br/>'; echo 'Short to Long: '; print_r(ggUrlAPI(0,'http://goo.gl/F90bfL')); echo '<br/><br/>'; ?>
3 要说明的地方1
从上面的测试结果图片可以看出,“长转短”和“短转长”的返回的对象中的成员类似,“短转长”多了一个status成员而已。下面是json格式的视图:
/* 1短网址转长网址 */ { "kind": "urlshortener#url", "id": "http://goo.gl/F90bfL", "longUrl": "http://www.huangkeye.cn" } /* 2短网址转长网址 */ { "kind": "urlshortener#url", "id": "http://goo.gl/F90bfL", "longUrl": "http://www.huangkeye.cn", "status": "OK" }
4 要说明的地方2
上面代码输出的是对象,大家可以直接输出里面的成员,例如输出“短转长”中返回的短网址和长网址
$result = ggUrlAPI(0,'http://goo.gl/F90bfL'); echo $result->id.$result->longUrl.'<br/><br/>';
5 错误响应Error responses
假如输入的数据有误或者其它原因造成接口返回错误数据,那么可以通过错误数据进行调试。谷歌短网址API返回的错误信息包括:a status code(状态码), a human readable message(人工可读的消息), and a list of error details(错误详细列表)。
{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Required", "locationType": "parameter", "location": "resource.longUrl" } ], "code": 400, "message": "Required" } }