我们经常使用file_get_contents函数来打开文件,实际上这个函数还可以打开一个网络地址,实现简单的网页抓取,用file-get-contents open file readfile等读取url时,会创建一个$http_response_header变量来保存http响应的报头,使用fopen等函数打开数据流信息可以使用stream_get_meta_data函数获取
$html = file_get_contents("http://baidu.com");
print_r($http_response_header);
/**
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 15 Nov 2014 16:14:36 GMT
[2] => Server: Apache
[3] => Cache-Control: max-age=86400
[4] => Expires: Sun, 16 Nov 2014 16:14:36 GMT
[5] => Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
[6] => ETag: "51-4b4c7d90"
[7] => Accept-Ranges: bytes
[8] => Content-Length: 81
[9] => Connection: Close
[10] => Content-Type: text/html
)
*/
$fp = fopen("http://baidu.com", "r");
print_r(stream_get_meta_data($fp));
/**
Array
(
[wrapper_data] => Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 15 Nov 2014 16:14:36 GMT
[2] => Server: Apache
[3] => Cache-Control: max-age=86400
[4] => Expires: Sun, 16 Nov 2014 16:14:36 GMT
[5] => Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
[6] => ETag: "51-4b4c7d90"
[7] => Accept-Ranges: bytes
[8] => Content-Length: 81
[9] => Connection: Close
[10] => Content-Type: text/html
)
[wrapper_type] => http
[stream_type] => tcp_socket/ssl
[mode] => r
[unread_bytes] => 81
[seekable] =>
[uri] => http://baidu.com
[timed_out] =>
[blocked] => 1
[eof] =>
)
*/