您好,欢迎访问一九零五行业门户网

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

今天看到一个朋友利用php socket来获取的301跳转之后地地址了,其实我们还有一个非常简单办法了,就是使用php get_headers()函数获取数获取http头信息了,下面来看看我们的实现方法.
获取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://phprm.com';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.phprm.com/ [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 )
文章地址:
转载随意^^请带上本文地址!
其它类似信息

推荐信息