0

I have this php code that consumes an api. I have one request that takes a long time almost 4 minutes. The curl request times out and returns an empty response.

I found this solution.

ini_set('max_execution_time', 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 

But this doesn't have any effect.

How do I wait for the request to finish execution. Here is full snippet.

ini_set('max_execution_time', 0);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49
  • Possible duplicate of [Setting Curl's Timeout in PHP](https://stackoverflow.com/questions/2582057/setting-curls-timeout-in-php) – Rotimi Oct 20 '18 at 14:33
  • This answer should solve your problem: https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout/94612 – Hektor Oct 20 '18 at 14:36
  • The answer in that question didn't work for me @Akintunde-Rotimi – Mubashar Abbas Oct 20 '18 at 14:37
  • There is a good chance your hosting provider is not letting you execute a single script for 4 minutes. Start bu dumping your current configuration with `phpinfo()` and check to see if your modifications are actually applied. – nikksan Oct 20 '18 at 14:40
  • @nikksan do the php.ini time out settings apply to the curl requests as well? – Mubashar Abbas Oct 20 '18 at 14:42
  • @MubasharAbbas the `max_execution_time` applies to anything you execute, regardless if its curl or something else – nikksan Oct 20 '18 at 14:44
  • can I use `ini_set` to set that value for specific requests.. or will have to change it in the .ini file. – Mubashar Abbas Oct 20 '18 at 14:46
  • A good explanation of the ini-set() function can be found here: http://php.net/manual/de/function.ini-set.php – Hektor Oct 20 '18 at 14:48
  • Okay `ini_set('max_execution_time', 0);` doesn't work either. – Mubashar Abbas Oct 20 '18 at 14:54

1 Answers1

1

I had this error too and had to change directly the default_socket_timeout in the php.ini file. After that, you have to reload Apache and it should work.