由於微博中有字數限制,所以如果你發的是網址,會自動將其變為短網址。之前我的幾篇文章介紹了如何將網址轉為短網址,這裡我們反過來,把短網址還原為實際網址。請參照下面的程序,用PHP實現:
<?php
$url = "http://163.fm/1QLJ8U";
echo unshorten($url);
function unshorten($url)
{
$url = trim($url);
$headers = get_headers($url);
$location = $url;
$short = false;
foreach($headers as $head)
{
if($head=="HTTP/1.1 302 Found")
$short = true;
if($short && startwith($head,"Location: "))
{
$location = substr($head,10);
}
}
return $location;
}
function startwith($Haystack, $Needle)
{
return strpos($Haystack, $Needle) === 0;
}
?>
程序運行結果如下:
http://www.bkjia.com/