42

I use curl to perform a HTTP request like this:

$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

$data = curl_exec($ch);
curl_close($ch);

How can I check if an error occurred, and whether it was a timeout error?

1
  • 1
    Curl or Divergence? Commented Dec 27, 2016 at 16:17

2 Answers 2

56

Use curl_errno()

Code 28 is timeout.

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

4 Comments

Which is named "CURLE_OPERATION_TIMEOUTED".
TIMEOUTED‽ Seriously, WTF is wrong with these people? Just to be clear, php.net's documentation (correctly) lists this unfortunate constant name. curl.haxx.se has better grammar but sadly php-curl will not recognize CURLE_OPERATION_TIMEDOUT.
@Lambart apparently CURLE_OPERATION_TIMEOUTED was the original constant name used by curl, which they then changed to CURLE_OPERATION_TIMEDOUT while keeping the former as a now-undocumented alias. The latest version of PHP supports both; see the comment at php.net/manual/en/curl.constants.php#117928 for some history.
@MarkAmery glad they finally got that fixed up.
4

you can check error number and its' description like this:

// Check if any error occurred
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}

1 Comment

if you use curl as an Object, it would be much nicer.

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.