Setting Curl'S Timeout in PHP

Set global CURL timeout

PHP's CURL uses the php.ini setting default_socket_timeout. The default value is 60, the unit is seconds.

PHP curl Connection timed out error

After struggling around 1 week and trying all suggested options finally I found that it was neither a connection issue nor API api-domain.com issue. Little strange for me but true that it's my SERVER scaling issue from which I am calling the API, I have increased the configuration (RAM and CPU) and found that the issue has been fixed.

Hope my experience with this issue help someone and save their time to troubleshoot.

Thanks, everyone for commenting and suggesting the solutions.

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.

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..

How to set curl timeout in Google API PHP client version 2.2.2

Try this:

$client->setConfig('CURLOPT_CONNECTTIMEOUT', 100);
$client->setConfig('CURLOPT_TIMEOUT', 1000);

have in mind that possible problem can be in their max execution time (which is if i good remember something between 3 and 5 mins)



Related Topics



Leave a reply



Submit