php取报头,php怎样猎取要求url时相应的报头信息_后端开发

php猎取要求url时相应的报头信息的要领:1、取url的时刻,建立名为【$http_response_header】的变量来保留http相应的报头;2、运用fopen翻开数据流信息,用【stream_get_meta_data】猎取。

2d0c4e100192dc115e96f3c2061ad06c.png

php猎取要求url时相应的报头信息的要领:

1、用file_get_contents或许fopen、file、readfile等函数读取url的时刻,会建立一个名 为$http_response_header的变量来保留http相应的报头.

示例代码一:

$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来猎取。

示例代码二:

$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的相应报文。

示例代码三:

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时相应的报头信息的细致内容,更多请关注ki4网别的相干文章!

收藏 | 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值