file_get_contents设置响应时间timeout的方法
curl有curlopt_connecttimeout可設(shè),fsockopen有$timeout可設(shè),而file_get_contents和fopen在打開url時(shí),都不可設(shè)置響應(yīng)時(shí)間timeout。如果url長時(shí)間沒有響應(yīng),file_get_contents 會(huì)跳過短時(shí)間內(nèi)沒有響應(yīng)的,而fopen會(huì)一直停留著等待,那么您的服務(wù)器就很可能掛了。
file_get_contents設(shè)置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);
?>
轉(zhuǎn)載于:https://blog.51cto.com/xu20cn/345527
總結(jié)
以上是生活随笔為你收集整理的file_get_contents设置响应时间timeout的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是 XML Web Service
- 下一篇: flash开发中如何实现界面代码分离