牛骨文教育服务平台(让学习变的简单)
博文笔记

用 file_get_contents 访问 http 时取回响应头

创建时间:2009-07-06 投稿人: 浏览次数:2904

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

 

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。