第七星尘的独立博客

新浪短链生成器——2012.11.9日更新,附上完整代码

新浪短链接转换提供一些API,详细可参考http://open.weibo.com/wiki/index.php?title=Short_url/shorten
其这里利用到的API,可以这样来举个实例:
http://api.t.sina.com.cn/short_url/shorten.json?source=1681459862&url_long=http://www.star7th.com/moresites/
用get的方式传递两个参数,source和url_long。这两个分别是appkey和要转换的网址。

点击上面的链接后,新浪方面会返回相对应的短地址,返回值具体如下:
[{"url_short":"http://t.cn/aCfAqt","url_long":"http://www.star7th.com/moresites/","type":0}]
在这段返回值中,短地址就是http://t.cn/aCfAqt。
所以只要把这段返回值中的那个的短地址给用户就行了。
在线示范:http://tools.star7th.com/tn/
完整代码如下:(可直接复制另存为index.php,放上php空间便可以运行)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<LINK media=screen href="http://outlink.sinaapp.com/fabuduan/images/account.css" type="text/css" rel="stylesheet">
<LINK media=screen href="http://outlink.sinaapp.com/fabuduan/images/header_bar.css" type="text/css" rel="stylesheet">
<title>获取授权码</title>
<style>
p {
text-indent: 2em;
line-height:1.7em;
}
.STYLE1 {color: #FF0000}
</style>
</head>
<body >
<div id="content">
<div id="post-shadow-bottom">
<div id="post-shadow-head">
</div>
<div class="post-inner" style="line-height:150%;font-size:13px;">
<br><br><p>
<?php
if(isset($_POST["lurl"]))
{
$long_url=$_POST["lurl"];
function shortenSinaUrl($long_url){
$apiKey=’1150019867′;
$apiUrl=’https://api.weibo.com/2/short_url/shorten.json?source=’.$apiKey.’&url_long=’.$long_url;
$curlObj = curl_init(); //初始化curl,当然,你也可以用fsockopen代替
curl_setopt($curlObj, CURLOPT_URL, $apiUrl); //设置网址
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); //将curl_exec的结果返回
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0); //是否输出返回头信息
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array(‘Content-type:application/json’)); //设置附加HTTP头
$response = curl_exec($curlObj); //执行
curl_close($curlObj); //关闭会话
$json = json_decode($response,true);
return $json[‘urls’][0][‘url_short’];
}
if ($long_url){
echo ‘
<table cellpadding=0 cellspacing=0 border=0 width=100% height=100%>
<tr> <td align= "center " valign= "middle ">
<div class="diy_label">生成的链接是  ’.shortenSinaUrl($long_url) .'</a>
</table>
‘;
}
}
else {
echo ‘
<table cellpadding=0 cellspacing=0 border=0 width=100% height=100%>
<tr> <td align= "center " valign= "middle ">
<form method="post">
<li class="diy_label">
<div align="center">请输入原地址
<input class="diy_text_l" name="lurl" type="text" />
<input class="diy_submit" name="submit" type="submit" value="生成" />
</div>
</li>
</form>
</table>
‘;
}
?>
</div>
</div>
</div>
</body>
</html>
 如无特殊说明,本站皆为原创。转载请注明来自第七星尘的独立博客《新浪短链生成器——2012.11.9日更新,附上完整代码》

评论

  1. livachen #1

    很好用,谢谢。

    回复
    2015-08-26
  2. xyayy #2

    出现一个问题,很长的链接缩短会只缩短一半

    回复
    2013-09-11
  3. 361电影网 #3

    挺好的。 试试看

    回复
    2013-01-25
  4. 立青 #4

    额…这个我试试了,没显示了,麻烦看下

    回复
    2011-11-1
  5. 免费资源部落 #5

    请问,这个sae空间可以修改文件的属性吗?比如777?

    回复
    2011-07-27