还原短链接的PHP代码

参考自一缸水C#还原短地址,原理是模拟请求短地址再获取header中的Location内容,就能拿到正确的页面url。

PHP代码:

function getRealUrl($shortUrl)
{
$header = get_headers($shortUrl, 1);
if ($header && $header['Location'])
{
$location = $header['Location'];
if (is_array($location))
{
return $location[0];
}
else
{
return $location;
}
}
return $shortUrl;
}
// For http://t.co/xxx
if (isset($_REQUEST['tco']))
{
$real_tco = getRealUrl('http://t.co/' . $_REQUEST['tco']);
}
// For full short URL
if (isset($_REQUEST['shorturl']))
{
$real_url = getRealUrl($_REQUEST['shorturl']);
}

参考源程序:realurl.php测试

Tags:

Leave a Reply


提醒: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。请务必注意user必须和评论者名相匹配(大小写一致)。