4

Is there a way to set the CURLOPT_CONNECTTIMEOUT somehow globally for all cURL requests?

I have an issue where after an upgrade to PHP 7.2 some software’s cURLs now fail at a 10s connect timeout when before they did not. I can't exactly say what changed this behavior, but I need to somehow globally set either a) what causes this to now time out, or b) increase the timeout limit.

Any php.ini settings that can affect this?

Edit: Setting default_socket_timeout does not seem to affect this.

Edit: FYI same issue with more Wordpress specifics: https://wordpress.stackexchange.com/questions/330013/curl-28-error-after-switch-from-to-brew-php-7-2-on-localhost

kontur
  • 4,934
  • 2
  • 36
  • 62
  • 1
    Possible duplicate of [Set global CURL timeout](https://stackoverflow.com/questions/29602256/set-global-curl-timeout) – A.J Alhorr Feb 27 '19 at 13:25
  • @A.JAlhorr 1) I am inquiring about `CONNECTTIMEOUT` not `TIMEOUT` and thus b) `CONNECTTIMEOUT` does not seem to respect that `php.ini` setting mentioned in that answer. – kontur Feb 27 '19 at 13:27
  • ye you're right. it takes 300 sec as a default value, but could't find where it gets that value from, probably the curl's dll. Well I found this on github, maybe it could help https://github.com/guzzle/guzzle/issues/652 – A.J Alhorr Feb 27 '19 at 13:57
  • @A.JAlhorr Thanks, but that solution seem to be application specific and only for that library (which probably in turn applies it to all cURL requests it makes) – kontur Feb 27 '19 at 18:56
  • 4
    The default 300 seconds is [hard-coded in the libcurl code](https://github.com/curl/curl/blob/8754ddb85daf0d4eeae985b1ba954745b6f360fd/lib/connect.h#L43). – Daniel Stenberg Mar 01 '19 at 21:04
  • Have you tried hardcoding the CURLOPT_CONNECTTIMEOUT option and does it work when you do that? The reason I ask is because people are saying that the default is 300 seconds but you said that it times out after 10 seconds. – Cave Johnson Mar 02 '19 at 20:28
  • Why don't you use wrapper for curl function or auto_prepend_file? – John Mar 04 '19 at 06:42
  • @KodosJohnson The 10s is a default set in WordPress, but I don’t see any change in the codebase there that would have caused this. Somehow this change in behavior was triggered by my PHP update, or so it seems. – kontur Mar 04 '19 at 06:52

1 Answers1

1

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..
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • 1
    I believe this is a viable work around. What appears to have caused this issue was a problem with my PC DNS settings that actually caused those connection timeouts. See https://wordpress.stackexchange.com/a/330409/27033 – kontur Mar 05 '19 at 09:26