Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

PHP has these two options related to timeout: CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT .

The descriptions on the PHP site are a bit vague. What's the difference?

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?

There also is CURLOPT_TIMEOUT_MS. I am not sure what happens when both CURLOPT_TIMEOUT_MS and CURLOPT_TIMEOUT are defined. I guess one should define either of the two. Sandeepan Nath Apr 19, 2016 at 7:25 @SandeepanNath From the CURL docs for CURLOPT_TIMEOUT , it states that, "If both CURLOPT_TIMEOUT and CURLOPT_TIMEOUT_MS are set, the value set last will be used." MrWhite Apr 4, 2017 at 8:01

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.

From Difference between CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT

I'd say, this is not really correct. Here it says, that CURLOPT_TIMEOUT defines the " the maximum time in seconds that you allow the libcurl transfer operation to take. ". This is a bit ambiguous. My experience seems to confirm Miloš answer below: CURLOPT_CONNECTTIMEOUT is really just for the connection phase and CURLOPT_TIMEOUT is for the transfer phase . Both added define the complete timeout of curl_exec() . Michael Härtl Jun 16, 2016 at 7:41 To make this clear let me add to this explanation. CURL_CONNECTIONTIMEOUT is the maximum time you can preserve the connection when you already connect. While CURL_TIMEOUT is the maximum time you can try from beginning till the end when connection can not be established. Faris Rayhan Oct 20, 2017 at 8:58 -1, the source used is a non-authoritative blog that is incorrect, as shown in its own comments there and in other answers here MestreLion Feb 26, 2019 at 23:40 As said above, CURL_CONNECTIONTIMEOUT is the connection phase, CURLOPT_TIMEOUT is everything from finalising connection to the end of data transmission. Common method might be to gradually increase connection timeout with each failed attempt but keep the transfer timeout high, so as to avoid waiting a long time for connections that are invalid Prodigle Apr 25, 2019 at 14:53 I scanned through the CURL source code to be sure, and it seems to confirm this answer. If only CURLOPT_CONNECTTIMEOUT is set, then only the connection phase is affected by it. If only CURLOPT_TIMEOUT is set, then it applies to the overall curl operation, including connect. However, if both are set, then the connect phase uses the shorter of both timeouts. See the function Curl_timeleft() in curl/lib/connect.c . MightyPork Jun 13, 2019 at 13:20

CURLOPT_CONNECTTIMEOUT is not a segment of the time represented by CURLOPT_TIMEOUT

If CURLOPT_CONNECTTIMEOUT is set to 3 seconds and CURLOPT_TIMEOUT to 4 seconds, execution may take up to 7 seconds.

I tested this by simulating slow server connecting (iptables drop).

I think I can confirm this. I have configured CURLOPT_TIMEOUT to 4 and leave CURLOPT_CONNECTTIMEOUT untouched. I also have PHP slow log configured for requests taking >10 s. My PHP script connects to a remote site and the slow log frequently reports problems with curl_exec() taking more than 10 s. I would have assumed that it is limited to 4 from the manual. Michael Härtl Jun 16, 2016 at 7:28 this is not true according to what I read in the source code, but if you verified it experimentally, that would suggest there are different implementations in different versions... The version on GitHub however hasn't changed this function in 12 years. MightyPork Jun 13, 2019 at 13:22

The accepted answer is incorrect. See the Everything CURL documentation for proper documentation.
Basically the connection time covers two aspects of establishing an http connection:

  • DNS resolution
  • Time until the tcp connection is established.
  • 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.

    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_CONNECTTIMEOUT is the the time to connect to the server only.

    CURLOPT_TIMEOUT is the whole time to connect plus the time to exchange data.

    So, CURLOPT_TIMEOUT includes CURLOPT_CONNECTTIMEOUT always.

    To verify that it's very easy using CURLINFO_CONNECT_TIME and 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.

    Of course, $yourMaxTotTime must be higher than $yourMaxConnTime always. All these values are in seconds.

    In addition to the accepted answer .

    According to the source code the 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;
          timeout_ms = data->set.connecttimeout;
        break;
    

    Unit tests for the source

    To those arguing whether CURLOPT_TIMEOUT includes CURLOPT_CONNECTTIMEOUT or not; I'll remind you that libcurl supports more than 23 different OSes, including DOS.

    Internet connections are incredibly complex (proxies, DNS resolution, IP connect, IP accept, SSH/SSL handshake, actual data transmission over a specific protocol like HTTP) and it may not always be possible for libcurl to honour exactly the timeouts requested.

    The rule of thumbs is that you should set CURLOPT_TIMEOUT >= CURLOPT_CONNECTTIMEOUT because that's what the manual says and libcurl will try to honour. Often with extreme accuracy.

    But on some platforms or edge cases you can easily run into situations where the actual timeout was CURLOPT_TIMEOUT + CURLOPT_CONNECTTIMEOUT; or heck, I even ran into situations where all timeouts are ignored because for a specific step in the connection process the OS/framework involved doesn't ask for a timeout value and won't return control to libcurl; and that's something beyond libcurl's control.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •