您的位置:首页 > 编程语言 > Go语言

利用google的短网址服务去实现简化网址

2013-07-01 16:27 218 查看
现在,随着微博的盛行,短网址(比如象:http://sinaurl.cn/7c00E这样的),
很流行了,而GOOGLE居然也出了这样的服务,下面讲解使用方法

1 注册有google account

2 申请google 的API KEY,

3 google的这个短网址服务在: http://code.google.com/intl/zh-CN/apis/urlshortener/index.html
4 可以在https://code.google.com/apis/console/中,开启你的短网址服务的API,
可惜的是,这个要。。。。自己想办法吧,之后GOOGLE会给你一个象下面连接的KEY https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=XXXXXXXXXXXXXXXXXX
5 PHP的程序代码如下:
<?php
$longURLData = array('longUrl' => 'http://gz.itownet.cn');

$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=你的key');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($longURLData));
$jsonResult = curl_exec($curl);
curl_close($curl);

$resultArray = json_decode($jsonResult, true);

echo '<br />Shortened URL: ' . $resultArray['id'];
echo '<br /><br />JSON: ' . $jsonResult;
?>

这样就可以得到你的短网址了,而且每天可以限制访问100万次哦,够了吧?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: