PHP Curl: Curlopt_Connecttimeout VS Curlopt_Timeout

PHP cURL: CURLOPT_CONNECTTIMEOUT vs CURLOPT_TIMEOUT

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

Good production values for CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT towards same server

You need to have information about the behaviour of that API in order to make an educated decision. It also depends on how that API is used in your code and what behaviour you desire in case of failure.

Consider:

  • If you set the value too low, your code will fail if the API takes longer to respond for whatever reason.
  • If the API takes longer to respond than ~4 seconds, what does that mean?

    • Is the API down entirely? Then indeed you do not need to wait longer.
    • Is it overloaded? Then would it have responded within 5 second? 6? 10? How long will this overloaded state last? Is it bad to compound it by overloading it more, or is it better to abort your code early and let the API recover?
    • Is there a temporary network hickup? Would that resolve if you made the timeout 5 seconds? 10? 20? How reliable is the network typically?
  • Would you prefer your app to be slow or dead if the API responds slowly for any of the above reasons?
  • Can you guarantee a certain response time of the API, and if it doesn't keep that guarantee that means it's down?
  • What's the target response time for your user facing site?

    Network timeouts are typically between 30 and 120 seconds, which compensates for temporary network issues but is also still within the range of a human. A user's attention typically drifts after a few hundred milliseconds, so you'll want to keep your response time well under one second. However, it's not unusual to have to wait for a website to load for a few seconds.

If you need to tweak such a critical parameter to find the best setting, the pragmatic way to do that is to set it to some initial value, get some measurement tools in place to monitor throughput and response rates, and initiate a stress test that simulates expected production level load. Then you'll see your error and response rates go up or down and can tweak the setting until you have found a value that satisfies your desired constraints.

PHP Catch cURL CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT events and take action

To get info about request, you can take a look on curl_getinfo or curl_multi_info_read

And use it something like it:

curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}

in an info array you can receive following data:

  • "url"
  • "content_type"
  • "http_code"
  • "header_size"
  • "request_size"
  • "filetime"
  • "ssl_verify_result"
  • "redirect_count"
  • "total_time"
  • "namelookup_time"
  • "connect_time"
  • "pretransfer_time"
  • "size_upload"
  • "size_download"
  • "speed_download"
  • "speed_upload"
  • "download_content_length"
  • "upload_content_length"
  • "starttransfer_time"
  • "redirect_time"
  • "certinfo"
  • "request_header"
    (This is only set if the CURLINFO_HEADER_OUT is set by a previous
    call to curl_setopt())

CURLOPT_CONNECTTIMEOUT is not working

CURLOPT_CONNECTTIMEOUT only sets the timeout for the amount of time it takes to connect. If you want to limit the amount of time the entire request is allowed to take, use CURLOPT_TIMEOUT instead.

How to set or circumvent CURLOPT_CONNECTTIMEOUT in PHP globally?

Any php.ini settings that can affect this?

don't think so. if you have PCEL Runkit installed and runkit.internal_override=1 in php.ini, then you could add this to a auto_prepend_file php.ini file, which should make your own timeouts the global defaults:

<?php
runkit_function_rename('curl_init','curl_original_init');
runkit_function_add ( 'curl_init' , '$url=null','$ch=curl_original_init($url);curl_setopt_array($ch,array(CURLOPT_TIMEOUT=>100,CURLOPT_CONNECT_TIMEOUT=>100));return $ch;');
  • note that many people would consider this to be an evil hack, technically changing the behavior of php builtin functions n all..

CURLOPT_TIMEOUT not working at all (php)

The curl_setopt($connection, CURLOPT_TIMEOUT, $seconds) should be called just right before curl_exec() function.

It wasn't working for me when I called it sooner

Curl force to Timeout

As per the documentation, CURLOPT_CONNECTTIMEOUT is an integer:

The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

If you have cURL >= 7.16.2 and PHP >= 5.2.3, you may use CURLOPT_CONNECTTIMEOUT_MS:

The number of milliseconds to wait while trying to connect. Use 0 to wait indefinitely. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.

Though, you should not confuse it with CURLOPT_TIMEOUT and CURLOPT_TIMEOUT_MS:

CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.
CURLOPT_TIMEOUT_MS - The maximum number of milliseconds to allow cURL functions to execute. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.

The obvious difference being that CURLOPT_CONNECTTIMEOUT is timeout before script dies if no connection is present; whilst CURLOPT_TIMEOUT kills the script after defined number of seconds.

best value for curl timeout and connection timeout

If you set it too high then your script will be slow as a one url that is down will take all the time you set in CURLOPT_TIMEOUT to finish processing. If you are not using proxies then you can just set the following values

CURLOPT_TIMEOUT = 3
CURLOPT_CONNECTTIMEOUT = 1

Then you can go through failed urls at a later time to double check on them.



Related Topics



Leave a reply



Submit