用 file_get_contents 访问 http 时取回响应头

来源:互联网 发布:日常开销记账软件 编辑:程序博客网 时间:2024/05/17 04:24

php 的 file_get_contents 可以直接用 url 读取网页,用来抓内容很方便。而且用 context 参数指定请求头。

不过在 file_get_contents 的手册页里没说怎么取得响应头。实际上,php 文件函数能访问 http 都是用的 stream wrapper 的东西,把不同形式的流包装成文件。在用 file_get_contents 访问 http 时,stream wrapper 会把响应头放到当前作用域下的 $http_response_header 数组变量里。

e.g.
file_get_contents('http://www.blogbus.com/');
var_dump($http_response_header);

参见:
http://www.php.net/manual/en/reserved.variables.httpresponseheader.php
http://www.php.net/manual/en/wrappers.http.php
http://www.php.net/manual/en/function.file-get-contents.php

 

原创粉丝点击