php中file_get_contents超时问题的解决方法
有时会遇到file_get_contents超时的问题,file_get_contents一步就做完了打开,读取,关闭的三个动作,过程相当自动化,并且可以读取远程内容。
不过,在网络状况差的情况下,可能会导致程序执行陷入停滞或者过慢,因为不停的重试和等待PHP进程本身的超时才会退出。
本文分享下我的解决方法:
就是创建一个可以控制的资源句柄,通过控制资源句柄超时来控制file_get_contents这个方法的超时时间。
例子:
$opts = array(
'http'=array(
'method'='GET',
'timeout'=1, //设置超时,单位是秒,可以试0.1之类的float类型数字
)
);
$context = stream_context_create($opts);
$contents = file_get_contents($url,false,$context);
方法不算完美,但可用,希望对大家有所帮助吧。
您可能感兴趣的文章:
php中file_get_contents超时问题的解决方法
PHP file_get_contents超时的设置方法
php 中file_get_contents超时问题的解决方法
PHP Warning: file_get_contents failed to open stream解决办法
phpmyadmin偶尔响应慢的问题的解决方法
php下载css中图片函数
php读取远程文件的三种方法分享
php中curl、fsocket、file_get_content函数比较
file_get_contents不能获取带端口的网址
PHP file_get_contents超时处理的设置方法