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

PHP获取短链接跳转后的真实地址和响应头信息的方法_php实例

获取到一个短连接,需要将短连接转换成真实的网址,通过查资料,发现 php 提供了一个函数 get_headers() ,可以完成这个任务,先把 头部信息获取到,然后再分析跳转地址即可:
复制代码 代码如下:
$url = 'http://t.cn/h5mwx';
$headers = get_headers($url, true);print_r($headers);
//输出跳转到的网址
echo $headers['location'];
附完整数组:
复制代码 代码如下:
array
(
    [0] => 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
        )    [server] => array
        (
            [0] => weibo
            [1] => bws/1.0
        )
    [content-length] => array
        (
            [0] => 203
            [1] => 16424
        )
    [date] => array
        (
            [0] => thu, 12 dec 2013 10:42:25 gmt
            [1] => thu, 12 dec 2013 10:42:25 gmt
        )
    [x-varnish] => 2893360335
    [age] => 0
    [via] => 1.1 varnish
    [connection] => array
        (
            [0] => close
            [1] => close
        )
)
附:get_headers函数官方文档
get_headers — 取得服务器响应一个 http 请求所发送的所有标头
说明
array get_headers ( string $url [, int $format = 0 ] )
get_headers() 返回一个数组,包含有服务器响应一个 http 请求所发送的标头。
参数
url:目标 url。
format:如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。
返回值
返回包含有服务器响应一个 http 请求所发送标头的索引或关联数组,如果失败则返回 false。
使用例子:
复制代码 代码如下:
$url = 'http://www.example.com';print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
以上例程的输出类似于:
复制代码 代码如下:
array
(
    [0] => 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
)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
)
其它类似信息

推荐信息