file_get_contents的超时处理对连接超时无效
来源:互联网 发布:联想网络控制工具 编辑:程序博客网 时间:2024/05/17 03:38
写PHP的朋友可能经常会用到file_get_contents,对于复杂一点的应用可能还会用下面的方法给file_get_contents加上超时处理
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>10,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.aipai.com', false, $context);
经过测试,这样的方法对于连接成功,但是后端输出/传输超时是没问题的。对于连接超时无效,file_get_contents函数会一直等到连接超时。
之前发现服务器上有一些CPU占用100%的PHP进程,是由于file_get_contents引起的,建议大家还是封装一个curl的函数或者类来用,类似这样
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,3);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$reval = curl_exec($ch);
curl_close($ch);
附:
PHP-CGI进程 CPU 100%与 file_get_contents函数的关系
- file_get_contents的超时处理对连接超时无效
- file_get_contents的超时处理(膘叔)
- PHP中file_get_contents($url)的超时处理
- PHP file_get_contents设置超时处理
- Mysql闲置超时,连接无效的问题
- TCP连接超时处理
- tcp连接超时处理
- file_get_contents 增加超时的时间限制
- file_get_contents 增加超时的时间限制
- file_get_contents 增加超时的时间限制
- file_get_contents 增加超时的时间限制
- file_get_contents 增加超时的时间限制
- Flink CEP 对超时事件的处理
- php file_get_contents超时解决
- file_get_contents fopen超时问题
- file_get_contents超时设置
- file_get_contents超时设置时间
- file_get_contents 超时 参数 stream_context_create()
- 利用Dom操作字符串一例
- ORA-14450: attempt to access a transactional temp table already in use
- android 手机屏蔽广告 hosts
- 十年磨一剑
- 提高String和StringBuffer性能的技巧
- file_get_contents的超时处理对连接超时无效
- 不能在控件上调用invoke Sql2008
- vc工作区中添加文件和文件夹
- 如何阅读源码
- Android 近百个项目的源代码,覆盖Android开发的每个领域
- 上海“智慧城市”3年增2万WiFi热点 速率达54M
- 编程练习-二叉树(父节点、非递归、O(0)空间)
- Ipad(IOS 5以前)的safari浏览器下, 如何实现单指滑动Scroll ( html + Jquery 实现)
- Xtext——6. 简单的五步来支持JVM语言