我们在开发过程中会请求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