fsockopen

PHP函数stream_set_timeout(Stream Functions)作用于读取流时

的时间控制。fsockopen函数的timeout只管创建连接时的超时,对于

连接后读取流时的超时,则需要用到 stream_set_timeout函数。由于

国内的网络环境不是很稳定,尤其是连接国外的时候,不想程序出现

Fatal error: Maximum execution time of 30 seconds exceeded in …的

错误,该函数尤其有用。stream_set_timeout需配合stream_get_meta_data

使用,如果没有timeout, stream_get_meta_data返回数组中time_out为空,

反之为1,可根据此判断是否超时。另外由于PHP默认的Maximum execution time

为30秒,这是一次执行周期的时间,为了不出现上述的Fatal error,还需要设置

一个总的读取流的时间,具体方法参见下面详细代码。

<?php
$fp=fopen("http://www.sina.com.cn", 'r');
$stream_meta = stream_get_meta_data($fp);
print_r($stream_meta);
?>

Array
(
     [wrapper_data] => Array
         (
             [0] => HTTP/1.0 200 OK
             [1] => Date: Tue, 06 Dec 2011 10:08:11 GMT
             [2] => Server: Apache
             [3] => Last-Modified: Tue, 06 Dec 2011 10:07:12 GMT
             [4] => Accept-Ranges: bytes
             [5] => X-Powered-By: mod_xlayout/rc2
             [6] => Cache-Control: max-age=60
             [7] => Expires: Tue, 06 Dec 2011 10:09:11 GMT
             [8] => Vary: Accept-Encoding
             [9] => X-UA-Compatible: IE=EmulateIE7
             [10] => Content-Type: text/html
             [11] => Age: 26
             [12] => Content-Length: 675274
             [13] => X-Cache: HIT from xd33-98.HP08040037.sina.com.cn
             [14] => Connection: close
         )

     [wrapper_type] => http
     [stream_type] => tcp_socket/ssl
     [mode] => r
     [unread_bytes] => 3759
     [seekable] => 
     [uri] => http://www.sina.com.cn
     [timed_out] => 
     [blocked] => 1
     [eof] => 
)
说明
array stream_get_meta_data ( int $fp )
返回现有 stream 的信息。可以是任何通过 fopen(), fsockopen() 和 pfsockopen()

建立的流。返回的数组包含以下项目:

原文地址:https://www.cnblogs.com/hehexu/p/8290426.html