tags:

views:

2369

answers:

2

Hi,

I'm using CURL to connect to multiple xml feeds and process them when the page loads. Unfortunately, every once in awhile a page won't be responsive and my script will stall as well. Here's an example of the code that I'm working with. I set the timeout to 1 but that doesn't appear to be working. I then set the timeout to 0.0001 just to test things today and it still pulled in xml feeds. Do you guys have any ideas on how to force curl to timeout when a script is taking forever.

             foreach($urls as $k => $v) {
   $curl[$k] = curl_init();   
                       curl_setopt($curl[$k], CURLOPT_URL, $v);
   curl_setopt($curl[$k], CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl[$k], CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($curl[$k],CURLOPT_CONNECTTIMEOUT, 1);

Thanks!

Russ

+1  A: 

See the difference between CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT

timdev
+3  A: 

There are two different timeouts with curl -- see curl_setopt manual's page :

CURLOPT_CONNECTTIMEOUT
The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

And :

CURLOPT_TIMEOUT
The maximum number of seconds to allow cURL functions to execute.

They both have a "millisecond" version : CURLOPT_CONNECTTIMEOUT_MS and CURLOPT_TIMEOUT_MS, respectively.


In your case, you might want to configure the second one too : what seems to take time is not the connection, but the construction of the feed on the server side.

Pascal MARTIN
Thanks so much this was extremely helpful. I'm trying to test to see if it works and to do this I set the timeout to 1 like in the code below. I am parsing xmls with 50-100 elements so I would imagine it would take more than a millisecond. I expected to get no results but instead everything returned as normal. curl_setopt($curl[$k], CURLOPT_TIMEOUT_MS, 1); Am I missing something? Thanks a lot PascalRuss
Arnold