I have a cURL call where I know the URL has been timing out (I get a 504 gateway timeout when I put the URL in my browser). I would like to handle this and present the user with a descriptive error, rather than just the 500 server error they are encountering currently.
I found this article but the answer isn’t working for me, at least the three ways I tried to implement it below. Not sure what I’m doing wrong? PHP curl timeout error detection
public function getDTRInfo($dtrID) {
$url = $this->cordraURL . "/objects/".$dtrID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$jsonResult = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
exit();
}
$curl_errno = curl_errno($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_errorn";
exit();
}
if($jsonResult===false) {
echo 'Curl error';
exit();
}
curl_close($ch);
$jsonAr = json_decode($jsonResult, true);
return $jsonAr;
}