PHP获取短链接跳转后的真实地址和响应头信息的方法
前端之家收集整理的这篇文章主要介绍了
PHP获取短链接跳转后的真实地址和响应头信息的方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@获取到一个短连接,需要将短连接转换成真实的网址,通过查资料,发现 PHP 提供了一个函数 get_headers() ,可以完成这个任务,先把 头部信息获取到,然后再分析跳转地址即可:
<div class="codetitle"><a style="CURSOR: pointer" data="21749" class="copybut" id="copybut21749" onclick="doCopy('code21749')"> 代码如下:
<div class="codebody" id="code21749">
$url = '
http://t.cn/h5mwx';
$headers = get
headers($url,TRUE);
@H502_0@print
r($headers);
@H502_0@//
输出跳转到的网址
echo $headers['Location'];
HTTP/1.1 302 Moved Temporarily
[Location] => http://www.baidu.com
[Content-Type] => Array
(
[0] => text/html;charset=UTF-8
[1] => text/html;charset=utf-8
)
@H_
502_0@ [Server] => Array
(
[0] => weibo
[1] => BWS/1.0
)
@H_
502_0@ [Content-Length] => Array
(
[0] => 203
[1] => 16424
)
@H_
502_0@ [Date] => Array
(
[0] => Thu,12 Dec 2013 10:42:25 GMT
[1] => Thu,12 Dec 2013 10:42:25 GMT
)
@H_
502_0@ [X-Varnish] => 2893360335
[Age] => 0
[Via] => 1.1 varnish
[Connection] => Array
(
[0] => close
[1] => Close
)
)
PHP
$url = 'http://www.example.com';
@H_
502_0@print_r(get_headers($url));
@H_
502_0@print_r(get_headers($url,1));
?>
HTTP/1.1 200 OK
[1] => Date: Sat,29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed,08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
@H_
502_0@Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat,29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed,08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)