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

来源:互联网 发布:淘宝网客户端电脑版 编辑:程序博客网 时间:2024/05/11 09:34
现在,随着微博的盛行,短网址(比如象: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万次哦,够了吧?
原创粉丝点击