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] => 
)
*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值