PHP cURL:CURLOPT_CONNECTTIMEOUT 与 CURLOPT_TIMEOUT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27776129/
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
PHP cURL: CURLOPT_CONNECTTIMEOUT vs CURLOPT_TIMEOUT
提问by texelate
PHP has these two options related to timeout: CURLOPT_CONNECTTIMEOUT
and CURLOPT_TIMEOUT
.
PHP 有这两个与超时相关的选项:CURLOPT_CONNECTTIMEOUT
和CURLOPT_TIMEOUT
.
The descriptions on the PHP site are a bit vague. What's the difference?
PHP 站点上的描述有点含糊。有什么不同?
To use a real world example: say you're sending GET vars to a URL via cURL and you want to receive a XML back, would CURLOPT_CONNECTTIMEOUT
relate to the maximum amount of time it can take to connect to the server and CURLOPT_TIMEOUT
the maximum amount of time it can take to send the XML back?
举一个真实的例子:假设你通过 cURL 向 URL 发送 GET vars 并且你想接收一个 XML,这CURLOPT_CONNECTTIMEOUT
与连接到服务器CURLOPT_TIMEOUT
所需的最长时间和最长时间有关可以将 XML 发回吗?
回答by Priyank
CURLOPT_CONNECTTIMEOUT is the maximum amount of time in seconds that is allowed to make the connection to the server. It can be set to 0 to disable this limit, but this is inadvisable in a production environment.
CURLOPT_TIMEOUT is a maximum amount of time in seconds to which the execution of individual cURL extension function calls will be limited. Note that the value for this setting should include the value for CURLOPT_CONNECTTIMEOUT.
In other words, CURLOPT_CONNECTTIMEOUT is a segment of the time represented by CURLOPT_TIMEOUT, so the value of the CURLOPT_TIMEOUT should be greater than the value of the CURLOPT_CONNECTTIMEOUT.
CURLOPT_CONNECTTIMEOUT 是允许连接到服务器的最长时间(以秒为单位)。可以将其设置为 0 以禁用此限制,但这在生产环境中是不可取的。
CURLOPT_TIMEOUT 是单个 cURL 扩展函数调用的执行将被限制的最长时间(以秒为单位)。请注意,此设置的值应包括 CURLOPT_CONNECTTIMEOUT 的值。
也就是说,CURLOPT_CONNECTTIMEOUT 是CURLOPT_TIMEOUT 表示的时间段,所以CURLOPT_TIMEOUT 的值应该大于CURLOPT_CONNECTTIMEOUT 的值。
From Difference between CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT
回答by Milo?
CURLOPT_CONNECTTIMEOUT is nota segment of the time represented by CURLOPT_TIMEOUT
CURLOPT_CONNECTTIMEOUT不是CURLOPT_TIMEOUT 表示的时间段
If CURLOPT_CONNECTTIMEOUT is set to 3 seconds and CURLOPT_TIMEOUT to 4 seconds, execution may take up to 7 seconds.
如果 CURLOPT_CONNECTTIMEOUT 设置为 3 秒,CURLOPT_TIMEOUT 设置为 4 秒,则执行可能需要长达 7 秒。
I tested this by simulating slow server connecting (iptables drop).
我通过模拟慢速服务器连接(iptables drop)对此进行了测试。
回答by Rasmus Larsen
The accepted answer is incorrect. See the Everything CURLdocumentation for proper documentation.
接受的答案是不正确的。有关正确的文档,请参阅Everything CURL文档。
Basically the connection time covers two aspects of establishing an http connection:
基本上连接时间包括建立http连接的两个方面:
- DNS resolution
- Time until the tcp connection is established.
- DNS解析
- 建立 tcp 连接之前的时间。
This period of time is NOT AT ALL covered by the CURLOPT_TIMEOUT or CURLOPT_TIMEOUT_MS options. These cover everything that happens after we start talking HTTP over the TCP connection that was just established in the connection phase.
CURLOPT_TIMEOUT 或 CURLOPT_TIMEOUT_MS 选项根本不涵盖这段时间。这些涵盖了我们在连接阶段刚刚建立的 TCP 连接上开始讨论 HTTP 之后发生的所有事情。
This distinction causes problems for lots of people, but it does allow one to set a relatively short connection timeout, because if the server is completely unavailable why wait for it? Yet you can still have your request timeout be reasonably long, in case expected response times for the service are hard to predict.
这种区别给很多人带来了问题,但它确实允许设置一个相对较短的连接超时,因为如果服务器完全不可用,为什么要等待呢?但是,您仍然可以将请求超时设置得相当长,以防服务的预期响应时间难以预测。
In general, for production setups, CURLOPT_CONNECTION_TIMEOUT should be less than 5 seconds and CURLOPT_TIMEOUT should be as low as possible (without causing you to regularly drop requests).
一般来说,对于生产设置, CURLOPT_CONNECTION_TIMEOUT 应该小于 5 秒, CURLOPT_TIMEOUT 应该尽可能低(不会导致您定期删除请求)。
回答by Timur Gaitov
In addition to the accepted answer.
除了公认的答案。
According to the source codethe settings are connected: if both are set, the most restrictive is used. But only in the connection stage.
根据源代码连接设置:如果两者都设置,则使用最严格的。但仅限于连接阶段。
/* if a timeout is set, use the most restrictive one */
if(data->set.timeout > 0)
timeout_set |= 1;
if(duringconnect && (data->set.connecttimeout > 0))
timeout_set |= 2;
switch(timeout_set) {
//...
case 3:
if(data->set.timeout < data->set.connecttimeout)
timeout_ms = data->set.timeout;
else
timeout_ms = data->set.connecttimeout;
break;
Unit testsfor the source
源代码的单元测试
回答by John
CURLOPT_CONNECTTIMEOUT is the the time to connect to the server only.
CURLOPT_CONNECTTIMEOUT 只是连接到服务器的时间。
CURLOPT_TIMEOUT is the whole time to connect plus the time to exchange data.
CURLOPT_TIMEOUT 是整个连接时间加上交换数据的时间。
So, CURLOPT_TIMEOUT includes CURLOPT_CONNECTTIMEOUT always.
因此, CURLOPT_TIMEOUT 始终包括 CURLOPT_CONNECTTIMEOUT。
To verify that it's very easy using CURLINFO_CONNECT_TIME and CURLINFO_TOTAL_TIME.
要验证使用 CURLINFO_CONNECT_TIME 和 CURLINFO_TOTAL_TIME 非常容易。
curl_getinfo($ch, CURLINFO_CONNECT_TIME) gets the info and curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $yourMaxConnTime) sets the max value to connect.
curl_getinfo($ch, CURLINFO_TOTAL_TIME) gets the info and curl_setopt($ch, CURLOPT_TIMEOUT, $yourMaxTotTime) sets the max value of the whole operation.
curl_getinfo($ch, CURLINFO_CONNECT_TIME) 获取信息, curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $yourMaxConnTime) 设置连接的最大值。
curl_getinfo($ch, CURLINFO_TOTAL_TIME) 获取信息, curl_setopt($ch, CURLOPT_TIMEOUT, $yourMaxTotTime) 设置整个操作的最大值。
Of course, $yourMaxTotTime must be higher that $yourMaxConnTime always. All these values are in seconds.
当然,$yourMaxTotTime 必须始终高于 $yourMaxConnTime。所有这些值都以秒为单位。