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

afoysal's avatar

Catch API error

I have some code like below

$response = Http::post('sometext/api/v1/user/login', [
                'email' => $request->email,
                'password' => $request->password
]);

Often I get error like below

ConnectionException
cURL error 6: Could not resolve host: 

How can I catch the error ?

Is it possible to catch the error using below code ?

if (empty($response)) {
	// some code
}
0 likes
1 reply
CorvS's avatar

Wrap it inside a try-catch block.

try {
    $response = Http::post('sometext/api/v1/user/login', [
        'email' => $request->email,
        'password' => $request->password
    ]);
} catch (Exception $exception) {
    // Do whatever
}
1 like

Please or to participate in this conversation.