curl有curlopt_connecttimeout可設,fsockopen有$timeout可設,而file_get_contents和fopen在打開url時,都不可設置響應時間timeout。若是url長時間沒有響應,file_get_contents 會跳太短時間內沒有響應的,而fopen會一直停留着等待,那麼您的服務器就極可能掛了。php
file_get_contents設置timeout的兩種方法:服務器
第一種方法:curl
<?phpsocket
$url='"http://www.zzsky.cn';ide
$timeout=10;//等待10秒url
$old_timeout=ini_get('default_socket_timeout');spa
ini_set('default_socket_timeout',$timeout);get
$contents=file_get_contents($url);it
ini_set('default_socket_timeout',$old_timeout);class
?>
第二種方法:
<?php
$url='"http://www.zzsky.cn';
$ctx=stream_context_create(array(
'http'=>array(
'timeout'=>10//等待10秒
)
)
);
return file_get_contents($url,0,$ctx);
?>