cURL Fatal Timeouts

Status
Not open for further replies.

Aequitas

New member
Feb 19, 2007
2,954
73
0
Canada
Fuck I can't figure this out for the life of me so maybe someone around here might have a solution cause god knows Google and the PHP docs don't.

Basically I'm running the shit out of cURL and using it to validate hundreds and sometimes thousands of RSS feeds at once but I noticed every now and again it'll hang up on me and it'll crash.

I found that on certain urls the page takes much too long to load and kills the script, now I do have the cURL timeout set and I played around with it lots and I've set up the set_time_limit() function correctly and I've even got things in place to prevent the browser from timing out and I've got things set up with sleep to give a slight cool down period just in case.

Basically I've traced it right down to this on certain URLs I try to grab the site but the site never gives a response, after a certain period of time (20 seconds for this but I would like it to be 10), the script kills itself because it could not get a response.

Now for some odd reason I can't figure out why it kills itself, when using timeouts on cURL if it reaches that timeout limit shouldn't it just stop waiting for a response and continue on executing your script instead of stopping it all together, thats what I would like it to do.

Any suggestions or thoughts on this would be great.

Thanks.
 


You'll probably have to put the code in a try-catch block. Then you can catch the exception (timeout I guess?) and continue executing your code as you want.
 
You'll probably have to put the code in a try-catch block. Then you can catch the exception (timeout I guess?) and continue executing your code as you want.

Correct me if I'm wrong but isn't try-catch for PHP 5 as where I'm running PHP 4.3
 
You're correct, PHP 4 doesn't have exceptions. Sorry about that.

You'll need to do something along these lines (I haven't actually tested this exact code). The important parts are setting CURLOPT_FAILONERROR to 0 and the if statement to test for an error.
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);

$incoming = curl_exec($ch);

if ( !$incoming )
{
print "curl_exec error:\n<blockquote>(" . curl_errno($ch) . ") "
. curl_error($ch) . "</blockquote>\n\n";
}
else
{
print "Success!';
 
You're correct, PHP 4 doesn't have exceptions. Sorry about that.

You'll need to do something along these lines (I haven't actually tested this exact code). The important parts are setting CURLOPT_FAILONERROR to 0 and the if statement to test for an error.
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);

$incoming = curl_exec($ch);

if ( !$incoming )
{
print "curl_exec error:\n<blockquote>(" . curl_errno($ch) . ") "
. curl_error($ch) . "</blockquote>\n\n";
}
else
{
print "Success!';

I just done a test with this and still nothing, see the whole problem is that I'm pretty sure if it gets a URL which never returns a response from the server it'll sit there and wait until it hits a timeout then once it hits that timeout it kills the script and doesn't toss an error.

It seems to end at curl_exec($ch); and it doesn't make it to the next lines of code, its as if the curl_exec function causes a fatal error which kills the script completely and prevents any further execution but the really tricky thing is that it doesn't even return any kind of error to me so I don't know maybe I'll try cutting things in half and running the validation after the script stops getting the feeds, that way it'll be like a fresh start of the script and I can know 100% if it was a PHP timeout in my php.ini file which I have no access to.
 
I know you probably have but just to make sure before I dig deeper you do have the following two lines in your code before the curl_exec right?
Code:
curl_setopt($ch, CURLOPT_TIMEOUT,$num_seconds);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $num_seconds);
 
Status
Not open for further replies.