您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

php如何获取请求url时响应的报头信息

2024/4/19 10:15:32发布3次查看
php获取请求url时响应的报头信息的方法:1、取url的时候,创建名为【$http_response_header】的变量来保存http响应的报头;2、使用fopen打开数据流信息,用【stream_get_meta_data】获取。
php获取请求url时响应的报头信息的方法:
1、用file_get_contents或者fopen、file、readfile等函数读取url的时候,会创建一个名 为$http_response_header的变量来保存http响应的报头.
示例代码一:
<?php $url = 'http://www.baidu.com'; $html = file_get_contents($url); print_r($http_response_header); //输出结果 array( [0] => http/1.1 200 ok [1] => date: tue, 06 nov 2012 08:51:01 gmt [2] => server: bws/1.0 [3] => content-length: 9803 [4] => content-type: text/html;charset=gbk [5] => cache-control: private [6] => expires: tue, 06 nov 2012 08:51:01 gmt [7] => set-cookie: baiduid=6635735b51b28640f425f802c49340f2:fg=1; expires=tue, 06-nov-42 08:51:01 gmt; path=/; domain=.baidu.com [8] => p3p: cp=" oti dsp cor iva our ind com " [9] => connection: close ) ?>
2、使用fopen等函数打开的数据流信息可以用 stream_get_meta_data来获取。
示例代码二:
<?php $fp = fopen($url, 'r'); print_r(stream_get_meta_data($fp)); fclose($fp); //输出结果 array ( [wrapper_data] => array ( [0] => http/1.1 200 ok [1] => date: tue, 06 nov 2012 08:54:22 gmt [2] => server: bws/1.0 [3] => content-length: 9803 [4] => content-type: text/html;charset=gbk [5] => cache-control: private [6] => expires: tue, 06 nov 2012 08:54:22 gmt [7] => set-cookie: baiduid=347578bcbd709f27925bdd8b05364a73:fg=1; expires=tue, 06-nov-42 08:54:22 gmt; path=/; domain=.baidu.com [8] => p3p: cp=" oti dsp cor iva our ind com " [9] => connection: close ) [wrapper_type] => http [stream_type] => tcp_socket [mode] => r [unread_bytes] => 0 [seekable] => [uri] => http://www.baidu.com [timed_out] => [blocked] => 1 [eof] => ) ?>
3、get_headers()也可以获取请求url的响应报文。
示例代码三:
<?php print_r(get_headers($url)); array ( [0] => http/1.1 200 ok [1] => date: tue, 06 nov 2012 08:58:41 gmt [2] => server: bws/1.0 [3] => content-length: 9803 [4] => content-type: text/html;charset=gbk [5] => cache-control: private [6] => expires: tue, 06 nov 2012 08:58:41 gmt [7] => set-cookie: baiduid=87b6f26eec74f2b8f7faba934dc6bb24:fg=1; expires=tue, 06-nov-42 08:58:41 gmt; path=/; domain=.baidu.com [8] => p3p: cp=" oti dsp cor iva our ind com " [9] => connection: close ) ?>
相关学习推荐:php图文教程
以上就是php如何获取请求url时响应的报头信息的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录