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

时间:2023-03-08 22:02:51

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

代码如下:

<?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;
}