<?php
/**
* @author: vfhky 20130304 20:10
* @description: PHP调用新浪短网址API接口
** @param string $type: 非零整数代表长网址转短网址,0表示短网址转长网址
*/
$type = $_GET['type'];
$url = $_GET['url'];
function xlUrlAPI($type,$url){
/* 这是我申请的APPKEY,大家可以测试使用 */
$key = '884790702';
if($type)
$baseurl = 'http://api.t.sina.com.cn/short_url/shorten.json?source='.$key.'&url_long='.$url;
else
$baseurl = 'http://api.t.sina.com.cn/short_url/expand.json?source='.$key.'&url_short='.$url;
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL,$baseurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$strRes=curl_exec($ch);
curl_close($ch);
$arrResponse=json_decode($strRes,true);
if (isset($arrResponse->error) || !isset($arrResponse[0]['url_long']) || $arrResponse[0]['url_long'] == '')
return 0;
if($type)
return $arrResponse[0]['url_short'];
else
return $arrResponse[0]['url_long'];
}
?>
<!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>
<link type="text/css" rel="stylesheet" href="http://www.waqiang.com/themes/url/style.css">
<title>新浪微博短网址生成器</title>
<script type="text/javascript">
function copyUrl2(obj)
{
var Url2=obj;//对象是URL
Url2.select(); //选择对象
document.execCommand("Copy"); //执行浏览器复制命令
alert("复制成功,直接在地址栏粘贴即可.");
}
</script>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
</head>
<body>
<div class="logo"><a href="index.php">新浪微博短网址</a></div>
<form action="t.php" method="get" target="_self">
<div id="warp">
<div id="tips">请在下面输入您需要转换的网址:</div>
<div id="input">
<input name="url" type="text" value="<?php echo $url; ?>" size="50" maxlength="999" id="url" class="input">
<input name="type" type="radio" value="1" />
短网址生成
<input name="type" type="radio" value="0" />
短网址还原 </div>
<div id="button">
<input type="submit" class="button" value="网 址 转 换">
</div>
<div id="input"> 新浪短网址:
<input type="text" value="<?php echo xlUrlAPI(1,$url); ?>" size="50" maxlength="999" id="url_short" class="input">
<input type="button" onClick="copyUrl2(document.getElementById('url_short'))" value=" 复制短网址 " />
</div>
<div id="input"> 还原长网址:
<input type="text" value="<?php echo xlUrlAPI(0,$url) ?>" size="50" maxlength="999" id="url" class="input">
</div>
<!-- <img src="http://qr.liantu.com/api.php?text=<?php echo xlUrlAPI(1,$url); ?>&el=h&w=200&m=5&logo=http://huabao.xrzxw.com/dm/taobao_logo.jpg"/> --> </div>
</form>
</body>
</html>