0

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_error\n";
            exit();
        }
        if($jsonResult===false) {
            echo 'Curl error';
            exit();
        }
        curl_close($ch);
        
        $jsonAr = json_decode($jsonResult, true);
        return $jsonAr;

    }

I also found this article and tried the following, but still get the 500 server error and not my echo statement. Handle "504 Gateway Time-out error" via php curl

    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);
        
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($httpCode == 504) {
           echo '504 Gateway timeout.  Please contact FEMC and report an issue with the cURL request to Cordra.';
           exit();
        } 
        curl_close($ch);
        
        $jsonAr = json_decode($jsonResult, true);
        return $jsonAr;

    }
4
  • Gateway timeout is not a curl timeout. The server is responding, it's telling you that it got an error further downstream. Commented Jun 4, 2024 at 20:03
  • You need to check for the 504 error code. Commented Jun 4, 2024 at 20:04
  • @Barmar, that's what I was trying to do at if ($curl_errno > 0), but I just get a 500 server error rather than my echo statement Commented Jun 5, 2024 at 12:55
  • See the linked question for how to get the HTTP response code. Commented Jun 5, 2024 at 14:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.