Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

grgbikash05's avatar

Handling ConnectionError exception in php?

Iam trying to post through an api using curl... I want to handle exceptions when the site is very busy and throws us connection refused or reset error.

There was ConnectionError exception in python. https://docs.python.org/3/library/exceptions.html#ConnectionError

Is there anything like that in php??

0 likes
9 replies
Nakov's avatar

You can handle it using try and catch, can't you?

try {
    // perform your curl here
} catch (Exception $ex) {
    // here you have the exception
}

Or check your log on which specific exception is thrown when you get connection refused and put that one instead of the Exception in the catch. But Exception is the one that all extend from so that will catch all exceptions.

Sinnbeck's avatar

What exception are you getting? And can you show your exception code handling code?

Nakov's avatar

Show some code please then on what you are trying to do, because I cannot understand the problem.

How do you get an exception then?

grgbikash05's avatar

while($success2 != 200) { $response2 = curl_exec($ch); $success2 = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); }

Suppose the site is too busy and it sends us connection refused.. The connection was reset.. then what to do in that sitution..

grgbikash05's avatar

is there any way to check connection refused or connection reset for demo purpose??

Nakov's avatar

@grgbikash05 demo or not demo:

while($success2 != 200) { 

    try {
       $response2 = curl_exec($ch); 
       $success2 = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); 
    } catch (\Exception $e) {
        // here the exception is CAUGHT :)
    }

}

Please or to participate in this conversation.