php file_get_contents() 是否有超时设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10236166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does file_get_contents() have a timeout setting?
提问by Flora Clinton
I am calling a series of links using the file_get_contents()method in a loop. Each link may take more than 15 minutes to process. Now, I worry about whether PHP's file_get_contents()has a timeout period?
我file_get_contents()在循环中使用该方法调用一系列链接。每个链接的处理时间可能超过 15 分钟。现在,我担心 PHP 是否file_get_contents()有超时期限?
If yes, it will time out with a call and move to next link. I don't want to call the next link without the prior one finishing.
如果是,它将通过呼叫超时并移动到下一个链接。我不想在没有完成前一个链接的情况下调用下一个链接。
So, please tell me whether file_get_contents()has a timeout period. The file which contains the file_get_contents()is set to set_time_limit()to zero (unlimited).
所以,请告诉我是否file_get_contents()有超时期限。包含 的文件file_get_contents()设置set_time_limit()为零(无限制)。
回答by stewe
The default timeout is defined by default_socket_timeoutini-setting, which is 60 seconds. You can also change it on the fly:
默认超时由default_socket_timeoutini-setting定义,即 60 秒。您也可以即时更改它:
ini_set('default_socket_timeout', 900); // 900 Seconds = 15 Minutes
Another way to set a timeout, would be to use stream_context_createto set the timeout as HTTP context optionsof the HTTP stream wrapperin use:
设置超时的另一种方法是将超时stream_context_create设置为正在使用的HTTP 流包装器的HTTP 上下文选项:
$ctx = stream_context_create(array('http'=>
array(
'timeout' => 1200, //1200 Seconds is 20 Minutes
)
));
echo file_get_contents('http://example.com/', false, $ctx);
回答by Randell
As @diyism mentioned, "default_socket_timeout, stream_set_timeout, and stream_context_create timeout are all the timeout of every line read/write, not the whole connection timeout." And the top answer by @stewe has failed me.
正如@diyism 提到的,“ default_socket_timeout、stream_set_timeout 和 stream_context_create 超时都是每行读/写的超时,而不是整个连接超时。”@stewe 的最佳答案让我失望。
As an alternative to using file_get_contents, you can always use curlwith a timeout.
作为 using 的替代方法file_get_contents,您始终可以使用curl超时。
So here's a working code that works for calling links.
所以这是一个用于调用链接的工作代码。
$url='http://example.com/';
$ch=curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result=curl_exec($ch);
curl_close($ch);
echo $result;
回答by NVRM
Yes!By passing a stream contextin the third parameter:
是的!通过在第三个参数中传递流上下文:
Here with a timeout of 1s:
这里超时为1s:
file_get_contents("https://abcedef.com", 0, stream_context_create(["http"=>["timeout"=>1]]));
Source in comment section of https://www.php.net/manual/en/function.file-get-contents.php
来源https://www.php.net/manual/en/function.file-get-contents.php 的评论部分
method
header
user_agent
content
request_fulluri
follow_location
max_redirects
protocol_version
timeout
Other contexts:https://www.php.net/manual/en/context.php
回答by Pascal Roget
It is worth noting that if changing default_socket_timeouton the fly, it might be useful to restore its value after your file_get_contentscall:
值得注意的是,如果动态更改default_socket_timeout,在调用file_get_contents后恢复其值可能会很有用:
$default_socket_timeout = ini_get('default_socket_timeout');
....
ini_set('default_socket_timeout', 10);
file_get_contents($url);
...
ini_set('default_socket_timeout', $default_socket_timeout);
回答by Max
For me work when i change my php.ini in my host:
对于我在主机中更改 php.ini 时的工作:
; Default timeout for socket based streams (seconds)
default_socket_timeout = 300

