PHP get_headers的用法

我们在开发过程中会请求URL并获取http请求发送的标头。比如查看301重定向的链接等信息

下面我们就模拟测试下get_header的使用方法

写一个重定向的测试文件,丢A服务器上

test.php

<?php
header( "Location: http://www.baidu.com" );
?>

另一个文件,丢B服务器上。也就是我们用来发起请求的文件

$url="http://www.123.com/test.php";
$result=get_headers($url,1);
print_r($result);

打印结果

Array
(
    [0] => HTTP/1.1 302 Found
    [Server] => Array
        (
            [0] => nginx/1.9.7
            [1] => BWS/1.1
        )

    [Date] => Array
        (
            [0] => Thu, 31 Oct 2019 09:33:13 GMT
            [1] => Thu, 31 Oct 2019 09:33:10 GMT
        )

    [Content-Type] => Array
        (
            [0] => text/html
            [1] => text/html
        )

    [Content-Length] => Array
        (
            [0] => 0
            [1] => 14615
        )

    [Connection] => close
    [X-Powered-By] => PHP/5.5.26
    [Location] => http://www.baidu.com
    [1] => HTTP/1.0 200 OK
    [Accept-Ranges] => bytes
    [Cache-Control] => no-cache
    [P3p] => Array
        (
            [0] => CP=" OTI DSP COR IVA OUR IND COM "
            [1] => CP=" OTI DSP COR IVA OUR IND COM "
        )

    [Pragma] => no-cache
    [Set-Cookie] => Array
        (
            [0] => BAIDUID=587693442FB88E961CDD443BEC6573EE:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
            [1] => BIDUPSID=587693442FB88E961CDD443BEC6573EE; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
            [2] => PSTM=1572514390; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
            [3] => BAIDUID=587693442FB88E96AC33252175C6E72D:FG=1; max-age=31536000; expires=Fri, 30-Oct-20 09:33:10 GMT; domain=.baidu.com; path=/; version=1; comment=bd
        )

    [Traceid] => 157251439004187625068283715721969339934
    [Vary] => Accept-Encoding
    [X-Ua-Compatible] => IE=Edge,chrome=1
)

我们可以看到最终得到HTTP返回码 302

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值