入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

用 file_get_contents 访问 http 时取回响应头

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

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

 

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。