stream_set_timeout的使用方法
2011-03-11 00:48
591 查看
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,还需要设置一个总的读取流的时间,具体方法参见下面详细代码。
$server="www.yahoo.com"; $port = 80; $data="GET / HTTP/1.0rn"; $data.="Connection: Closern"; $data.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)rnrn"; $start_time = time(); $fp=fsockopen($server, $port, $errno, $errstr, 5); if (!$fp) { die("Connect Timeout.n"); } else { stream_set_blocking($fp, True); stream_set_timeout($fp, 3); fputs($fp, "$data"); while (!feof($fp)) { $text .= fread($fp, 2000); $diff = time() - $start_time; if ($diff > 24) { die("Timeout!n"); } $status = stream_get_meta_data($fp); if ($status[’timed_out’]) { die("Stream Timeout!n"); } } } fclose($fp);
相关文章推荐
- stream_set_timeout的使用方法
- stream_set_timeout的使用方法
- Socket.setSoTimeout()方法的使用
- fscokopen 中执行超时 使用stream_set_timeout设置超时
- Socket.setSoTimeout()方法的使用
- mysql 高版本jdbc驱动程序 使用PreparedStatement.setBinaryStream方法报错及解决办法
- setTimeout/setInterval的使用和参数调用方法
- jQuery setTimeout()函数使用方法
- jQuery setTimeout()函数使用方法
- JavaScript setTimeout和setInterval的使用方法 说明
- Socket.setSoTimeout()方法的使用
- C++文件写入、读出函数,ofstream的使用方法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Mini2440开发板自带的mjpg‐stream使用方法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- ieda使用 在jsp页面中,有时候会出现不能智能显示方法 idea pageContext.setAttribute
- 使用LocalStorage、sessionStorage报异常DOMException: Failed to execute 'setItem' on 'Storage': 解决方法
- Unity3D中Quaternion类SetLookRotation方法和LookRotation方法使用解析
- C++ stringstream介绍,使用方法与例子