file_get_contents设置响应时间timeout的方法
curl有curlopt_connecttimeout可設,fsockopen有$timeout可設,而file_get_contents和fopen在打開url時,都不可設置響應時間timeout。如果url長時間沒有響應,file_get_contents 會跳過短時間內沒有響應的,而fopen會一直停留著等待,那么您的服務器就很可能掛了。
file_get_contents設置timeout的兩種方法:
第一種方法:
<?php
$url='"http://www.zzsky.cn';
$timeout=10;//等待10秒
$old_timeout=ini_get('default_socket_timeout');
ini_set('default_socket_timeout',$timeout);
$contents=file_get_contents($url);
ini_set('default_socket_timeout',$old_timeout);
?>
第二種方法:
<?php
$url='"http://www.zzsky.cn';
$ctx=stream_context_create(array(
'http'=>array(
? ? ? ? 'timeout'=>10//等待10秒
? ? ? ?)
? ? )
);
return file_get_contents($url,0,$ctx);
?>
轉載于:https://blog.51cto.com/xu20cn/345527
總結
以上是生活随笔為你收集整理的file_get_contents设置响应时间timeout的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是 XML Web Service
- 下一篇: flash开发中如何实现界面代码分离