stream_set_timeout的使用方法
来源:互联网 发布:linux 实验三 vi编辑器 编辑:程序博客网 时间:2024/05/18 03:13
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);
0 0
- stream_set_timeout的使用方法
- stream_set_timeout的使用方法
- stream_set_timeout的使用方法
- stream_set_timeout的使用方法
- 的使用方法
- ctags使用方法 ctags的使用方法
- UITabBarController的使用方法、 UINavigationController的使用方法、 presentModalViewController的使用方法
- UITabBarController的使用方法 UINavigationController的使用方法
- ctags使用方法 ctags的使用方法1
- DateTime的一些使用方法
- CSDNBlog的使用方法
- RetroGuard的使用方法(转)
- 一些Blog的使用方法
- RAR的命令行使用方法
- VI的使用方法
- Make的使用方法
- COM组件的使用方法
- AgileFlow的使用方法
- 【玩转cocos2d-x之三十五】Earth Warrior 3D大揭秘
- PAT 1051. Pop Sequence (25)
- 浏览器跨域问题 jsonp
- 看log的重要性
- android开发动画总结一(tweened animation)
- stream_set_timeout的使用方法
- JAVA学习数据结构
- Fraction to Recurring Decimal
- 分数加减法 nyoj
- 【玩转cocos2d-x之三十六】Flappy Bird大集结
- C Primer Plus 第五章:运算符、表达式和语句
- Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- android中使用startActivityForResult回传数据
- Windows7 下安装 Tomcat + Solr5.2