http系列一

我们经常使用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] => 
)
*/




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。