PHP file_get_contents 函数超时的几种解决方法
这里就简单介绍两种:
一、增加超时的时间限制
这里需要注意:set_time_limit只是设置你的php程序的超时时间,而不是file_get_contents函数读取url的超时时间。
我一开始以为set_time_limit也能影响到file_get_contents,后来经测试,是无效的。真正的修改file_get_contents延时可以用resource $context的timeout参数:
$opts = array(
‘http'=>array(
‘method'=>”get”,
‘timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.example.com', false, $context);
fpassthru($fp);
二、一次有延时的话那就多试几次 有时候失败是因为网络等因素造成,没有解决办法,但是可以修改程序,失败时重试几次,仍然失败就放弃,因为file_get_contents()如果失败将返回 false,所以可以下面这样编写代码:
$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http…'))===false) $cnt++;
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
网安追踪流程
1.不用代理网民发帖流程:发帖人 → ISP → 服务器托管商 → 服务器网警追踪流程:网监 → 服务器IP →...
[阅读全文]
-
-
-
HTTP状态码大全
转自:https://www.cnblogs.com/lxinxuan/archive/2009/10/22/1...
[阅读全文]
-
-
-
-
-
网友评论