PHP如何通过URL访问,获得新的URL 两种方法

1.1

1 $url = 'http://passport.drcloud.cn/api/logon.asp?id=1&ru=http://203.158.158.122/store/thirdLogin.php&tw=1800&sm=0&lc=2052';
2 $newurl = getMapUrl($url);
3 echo $newurl;
4  
5 function getMapUrl($url){
6     $result = get_headers($url, true);
7     return isset($result['Location'])? $result['Location'] : '';
8 }

1.2

 1 $url = 'http://passport.drcloud.cn/api/logon.asp?id=1&ru=http://203.158.158.122/store/thirdLogin.php&tw=1800&sm=0&lc=2052';
 2 $newurl = getMapUrl($url);
 3 echo $newurl;
 4 
 5 function getMapUrl($url){
 6     file_get_contents($url);
 7     $result = $http_response_header;
 8     if($result){
 9         foreach($result as $val){
10             if(substr($val,0,10)=='Location: '){
11                 return str_replace('Location: ','', $val);
12             }
13         }
14     }
15     return '';
16 }

2

 1 $url = 'http://passport.drcloud.cn/api/logon.asp?id=1&ru=http://203.158.158.122/store/thirdLogin.php&tw=1800&sm=0&lc=2052';
 2 $newurl = getHeadersNew($url);
 3 var_dump($newurl);
 4 function getHeadersNew($url){ 
 5     $ch= curl_init(); 
 6     curl_setopt($ch, CURLOPT_URL, $url); 
 7     curl_setopt($ch, CURLOPT_NOBODY, 1); 
 8     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 9     curl_setopt($ch, CURLOPT_HEADER, 1); 
10     $f=curl_exec($ch); 
11     curl_close($ch); 
12     $h=explode("
",$f); 
13     $r=array(); 
14     foreach( $h as $t){ 
15       $rr=explode(":",$t,2); 
16       if(count($rr)==2 ){ $r[$rr[0]]=trim($rr[1]);} 
17     } 
18     return $r; 
19 }
原文地址:https://www.cnblogs.com/liruning/p/6077036.html