php短域名转为实际域名的函数参考

将实际域名转换为短域名,有时也要反转查看下实际域名,可以参考如下的函数。

代码如下:

<?php
/**
* php短域名互转
* edit by www.jbxue.com
* 最后修改日期:2013-7-6
*/
$url = "http://sinaurl.cn/hbdsU5"; 
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; 
}

 

posted @ 2013-07-07 21:36  un123  阅读(237)  评论(0编辑  收藏  举报