php短域名转换为实际域名函数 浪峰小园子

php短域名转换为实际域名函数

现在很多朋友需要将实际域名转换为短域名,但也有朋友需要反转查看下实际域名,那么就可以使用这个函数。

 1 $url = "http://sinaurl.cn/hbdsU5"; 
 2 echo unshorten($url); 
 3 function unshorten($url) { 
 4 $url = trim($url); 
 5 $headers = get_headers($url); 
 6 $location = $url; 
 7 $short = false; 
 8 foreach($headers as $head) { 
 9 if($head=="HTTP/1.1 302 Found") $short = true; 
10 if($short && startwith($head,"Location: ")) { 
11 $location = substr($head,10); 
12 } 
13 } 
14 return $location; 
15 } 
16 function startwith($Haystack, $Needle){ 
17 return strpos($Haystack, $Needle) === 0; 
18 } 

 源地址:http://laf.freel.cn/default/phpshorturl.html

 
原文地址:https://www.cnblogs.com/rirber/p/3075212.html