0

I have the CURL request below, and it is returning a 504 error. I want to echo out the fact that it is timing out to the UI, right now the UI isn't notified, but the Chrome console is. Is there a way I can grab the 504 and drop it in an if statement:

if ($error == 504) {
    // tell the UI it timed out
} else {
    // report the response
}

Right now I am using:

curl_getinfo($curl, CURLINFO_HTTP_CODE)

but it doesn't seem to grab the 504, probably because there was no response.

curl_setopt_array($curl, array(
    CURLOPT_URL => $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 60000,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERAGENT => $userAgent,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $payload,
    CURLINFO_HEADER_OUT => true,
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Authorization: Basic xxx'
    ),
));
Ping
  • 191
  • 1
  • 12
  • If there's no response, where would the 504 error code come from? – Barmar Feb 16 '23 at 21:35
  • `504` is `Gateway Timeout`. It comes from a proxy that gets a timeout when it tries to connect to the target server. It's not the error when the original connection gets a timeout. – Barmar Feb 16 '23 at 21:37
  • curl_exec($curl) is not returning anything, it echos as empty, but I get a 504 in the javascript console. – Ping Feb 16 '23 at 21:51
  • The browser is synthesizing that in the console, it's not a real error code. – Barmar Feb 16 '23 at 21:53
  • So maybe set CURLOPT_TIMEOUT higher and wat for an HTTP response? – Ping Feb 16 '23 at 21:54
  • I set the CURLOPT_TIMEOUT to 120000 but I get the browser 504 after about 15 seconds. – Ping Feb 16 '23 at 21:57
  • Use `CURLOPT_CONNECTTIMEOUT` – Barmar Feb 16 '23 at 21:58
  • Does this answer your question? [Handle "504 Gateway Time-out error" via php curl](https://stackoverflow.com/questions/47180816/handle-504-gateway-time-out-error-via-php-curl) – Nico Haase Mar 16 '23 at 09:07

1 Answers1

0

curl_getinfo($curl, CURLINFO_HTTP_CODE)....but it doesn't seem to grab the 504

Barmar suggested that this question is a duplicate of PHP curl timeout error detection hpwever that describes a timeout implemented on the HTTP client - if you specifically want to get the timeout from the gateway/proxy you need to wait a bit longer (assuming it is online).

Most likely your default_socket_timeout is shorter than the timeout on the proxy. PHP is terminating the Curl call before it gets a response from the remote end. If unset, the default_socket_timeout is 60 seconds. Do beware that most browsers will timeout at 5 minutes after the initial HTTP request.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • Hey, so I have the timeout set to 60 seconds, I have tried 90 and 120 too. I am getting the 504 error in the javascript console, but my question is when that arrived, how do I detect that with PHP, so I can pass it to the UI and say "it timed out"? – Ping Feb 16 '23 at 21:48
  • Because right now the spinner in the UI just keeps spinning. I want to stop it and say "timeout" is there some value I can capture to pass in the AJAX response? – Ping Feb 16 '23 at 21:49
  • If you've increased your timeout to greater than the value you measured when you tested this directly with a browser then either there is a problem in your code or the site you are trying to retrieve content from is behaving differently with your Curl request. Maybe it's anti-leech mechanism. – symcbean Feb 16 '23 at 21:55
  • So the use case is the remote endpoint isn't responding, but the developer says it is. My client is complaining so I built a UI so he can push a button and see it not work. I am trying to have the UI say "it timed out" when a timeout response is received. – Ping Feb 16 '23 at 21:58
  • I get 504 in Postman too. – Ping Feb 16 '23 at 21:58
  • Maybe Barmar was right and you're asking the XY Problem. – symcbean Feb 16 '23 at 22:00
  • I changed it to CURLOPT_CONNECTTIMEOUT at 120000 and I am now getting a response at least though it has no response code. – Ping Feb 16 '23 at 22:10