Aug 15, 2021
5
Level 3
Catch Laravel Http errors
I'm using Laravel Http to call an API and get a list of the latest transactions. I can check the errors like failed authentication or such using failed(); or successful(); methods but errors like failed to connect to the API like the cURL error 6 cant be handled that way apparently and it throws the error page. I would like to be able to handle errors like API URL couldn't be resolved or such errors that cURL throws in a simple manner just to know there has been an error and show a custom message instead.
public function index()
{
$status = "100";
$page = "0";
$page_size = "10";
$idpay = Http::withHeaders([
'Content-Type' => 'application/json',
'X-API-KEY' => 'xxxxxx',
'X-SANDBOX' => '1'
])->post("https://api.idpay.ir/v1.1/payment/transactions?page=$page&page_size=$page_size");
if ($idpay->successful())
{
return view('supporters')->with('idpay', $idpay->json())->with('idpayFailed', false);
}
elseif ($idpay->failed()){
return view('supporters')->with('idpayFailed', true);
}
}
Please or to participate in this conversation.