PHP获取301重定向页面跳转后真实URL地址

作者:简简单单 2014-07-01

获取301状态肯定没问题。

 代码如下 复制代码

function getrealurl($url){
 $header = get_headers($url,1);
 if (strpos($header[0],'301') || strpos($header[0],'302')) {
  if(is_array($header['Location'])) {
   return $header['Location'][count($header

['Location'])-1];
  }else{
   return $header['Location'];
  }
 }else {
  return $url;
 }
}

补充:get_headers — 取得服务器响应一个 HTTP 请求所发送的所有标头

 代码如下 复制代码

$url = 'http://111com.net';

print_r(get_headers($url));

结果

 代码如下 复制代码

Array
(
    [0] => HTTP/1.1 301 Moved Permanently
    [1] => Date: Tue, 01 Jul 2014 07:49:26 GMT
    [2] => Server: Apache/2.2.22 (Win32) PHP/5.2.17
    [3] => Location: http://www.111com.net/
    [4] => Content-Length: 314
    [5] => Content-Type: text/html; charset=iso-8859-1
    [6] => X-Via: 1.1 jszjsx60:8080 (Cdn Cache Server V2.0), 1.1 zb51:6 (Cdn Cache Server V2.0)
    [7] => Connection: close
    [8] => HTTP/1.1 200 OK
    [9] => Date: Tue, 01 Jul 2014 07:49:27 GMT
    [10] => Server: Apache/2.2.22 (Win32) PHP/5.2.17
    [11] => Last-Modified: Tue, 01 Jul 2014 07:41:43 GMT
    [12] => ETag: "7a0000002fe1a1-68a9-4fd1ce83bc0f7"
    [13] => Accept-Ranges: bytes
    [14] => Content-Length: 26793
    [15] => Content-Type: text/html
    [16] => X-Via: 1.1 jszjsx60:8080 (Cdn Cache Server V2.0), 1.1 zb62:5 (Cdn Cache Server V2.0)
    [17] => Connection: close
)

相关文章

精彩推荐