0

I'm developing a script where I'm using PHP and cURL to fetch data from a REST web service.

My problem is that curl_exec often times out in the PHP script, but when I try the same request from the command line cURL client I get an instant answer.

It seems like the PHP cURL somehow caches this timeout for some time and then suddenly decides that it's going to fetch the data I need. Multiple reloads produces the same result: timeout. And after a few minutes everything works fine. The command line cURL client has been getting data all the time.

What can be wrong?

  • Have a look at this [answer](http://stackoverflow.com/a/14436877/662732) on how you can get some debug information from cURL. – fab Jun 02 '14 at 09:03

1 Answers1

0

Try to use this curl setting:
curl_setopt($curl_handler, CURLOPT_FRESH_CONNECT, true);

Also this setting could be useful, as it forces connection to close after the request is completed:
curl_setopt($curl_handler, CURLOPT_FORBID_REUSE, true);

fortune
  • 3,361
  • 1
  • 20
  • 30
Ion Dulgheru
  • 129
  • 1
  • 7
  • I tried that, but unfortunately it does not work :( It seems a bit more stable, but the error is still there, just to a lesser degree. – user3698625 Jun 02 '14 at 12:13
  • Also DNS caching could be an issue.Try to disable the DNS caching with the following setting: curl_setopt($curl_handler, CURLOPT_DNS_CACHE_TIMEOUT, 0);
    Also did you try to use CURLOPT_VERBOSE for debugging? As @fab suggested previously?
    – Ion Dulgheru Jun 02 '14 at 12:34