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);
9
  • Possible duplicate of Setting Curl's Timeout in PHP Commented Oct 20, 2018 at 14:33
  • This answer should solve your problem: unix.stackexchange.com/questions/94604/does-curl-have-a-timeout/… Commented Oct 20, 2018 at 14:36
  • The answer in that question didn't work for me @Akintunde-Rotimi Commented Oct 20, 2018 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. Commented Oct 20, 2018 at 14:40
  • @nikksan do the php.ini time out settings apply to the curl requests as well? Commented Oct 20, 2018 at 14:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.