获取http头部信息三种方法

来源:互联网 发布:闻喜天际网络 编辑:程序博客网 时间:2024/05/30 02:52

第一种:php自带函数 get_headers($url);

 Array ( [0] => HTTP/1.1 200 OK [1] => Date: Wed, 06 May 2015 02:34:12 GMT [2] => Content-Type: text/html [3] => Content-Length: 14613 [4] => Last-Modified: Wed, 03 Sep 2014 02:48:32 GMT [5] => Connection: Close [6] => Vary: Accept-Encoding [7] => Set-Cookie: BAIDUID=F9BCE7712802900E21F00B376B839D9A:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [8] => Set-Cookie: BIDUPSID=F9BCE7712802900E21F00B376B839D9A; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [9] => Set-Cookie: BDSVRTM=0; path=/ [10] => P3P: CP=" OTI DSP COR IVA OUR IND COM " [11] => Server: BWS/1.1 [12] => X-UA-Compatible: IE=Edge,chrome=1 [13] => Pragma: no-cache [14] => Cache-control: no-cache [15] => BDPAGETYPE: 1 [16] => BDQID: 0xeb9ecf4900010c23 [17] => BDUSERID: 0 [18] => Accept-Ranges: bytes )

第二种:打印$http_response_header变量。该变量在脚本中调用get_headers 或者使用file_get_contents($url后生效)


array(19) {  [0]=>  string(15) "HTTP/1.1 200 OK"  [1]=>  string(35) "Date: Wed, 06 May 2015 02:35:41 GMT"  [2]=>  string(23) "Content-Type: text/html"  [3]=>  string(21) "Content-Length: 14613"  [4]=>  string(44) "Last-Modified: Wed, 03 Sep 2014 02:48:32 GMT"  [5]=>  string(17) "Connection: Close"  [6]=>  string(21) "Vary: Accept-Encoding"  [7]=>  string(141) "Set-Cookie: BAIDUID=C61A3BFDD7F16D71D795DA2143256829:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"  [8]=>  string(137) "Set-Cookie: BIDUPSID=C61A3BFDD7F16D71D795DA2143256829; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"  [9]=>  string(29) "Set-Cookie: BDSVRTM=0; path=/"  [10]=>  string(39) "P3P: CP=" OTI DSP COR IVA OUR IND COM ""  [11]=>  string(15) "Server: BWS/1.1"  [12]=>  string(33) "X-UA-Compatible: IE=Edge,chrome=1"  [13]=>  string(16) "Pragma: no-cache"  [14]=>  string(23) "Cache-control: no-cache"  [15]=>  string(13) "BDPAGETYPE: 1"  [16]=>  string(25) "BDQID: 0xd7afaadd00012267"  [17]=>  string(11) "BDUSERID: 0"  [18]=>  string(20) "Accept-Ranges: bytes"}

第三种:使用fread fopen之类函数,然后用stream_get_meta_data函数获取打开文件数据流信息 


array(10) {  ["wrapper_data"]=>  array(19) {    [0]=>    string(15) "HTTP/1.1 200 OK"    [1]=>    string(35) "Date: Wed, 06 May 2015 02:37:09 GMT"    [2]=>    string(23) "Content-Type: text/html"    [3]=>    string(21) "Content-Length: 14613"    [4]=>    string(44) "Last-Modified: Tue, 02 Sep 2014 08:55:13 GMT"    [5]=>    string(17) "Connection: Close"    [6]=>    string(21) "Vary: Accept-Encoding"    [7]=>    string(141) "Set-Cookie: BAIDUID=F373561ACB508F0628B997C7C6ECCDEB:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"    [8]=>    string(137) "Set-Cookie: BIDUPSID=F373561ACB508F0628B997C7C6ECCDEB; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"    [9]=>    string(29) "Set-Cookie: BDSVRTM=0; path=/"    [10]=>    string(39) "P3P: CP=" OTI DSP COR IVA OUR IND COM ""    [11]=>    string(15) "Server: BWS/1.1"    [12]=>    string(33) "X-UA-Compatible: IE=Edge,chrome=1"    [13]=>    string(16) "Pragma: no-cache"    [14]=>    string(23) "Cache-control: no-cache"    [15]=>    string(13) "BDPAGETYPE: 1"    [16]=>    string(25) "BDQID: 0xb8cdbdd0000149cc"    [17]=>    string(11) "BDUSERID: 0"    [18]=>    string(20) "Accept-Ranges: bytes"  }  ["wrapper_type"]=>  string(4) "http"  ["stream_type"]=>  string(10) "tcp_socket"  ["mode"]=>  string(1) "r"  ["unread_bytes"]=>  int(0)  ["seekable"]=>  bool(false)  ["uri"]=>  string(20) "http://www.baidu.com"  ["timed_out"]=>  bool(false)  ["blocked"]=>  bool(true)  ["eof"]=>  bool(false)}


php脚本

<?php// $arr=get_headers('http://www.baidu.com');// print_r($arr);// $html=file_get_contents('http://www.baidu.com');// var_dump($http_response_header);$fp=fopen('http://www.baidu.com','r');// var_dump($http_response_header);var_dump(stream_get_meta_data($fp));




0 0