Search code examples
phpcurlhttp-status-code-504

Handle "504 Gateway Time-out error" via php curl


I am scraping data for many items via php curl, But for few items, the target website send "504 Gateway Time-out nginx" error, I have no problem with that, But the main problem is it halted the whole script, So script stops after this error and don't scrape next items,

I just wanna ignore(handle) that error, So that it doesn't halt the script.

This link can be helpful to understand the question https://serverfault.com/questions/882421/504-gateway-time-out-nginx-on-apache-server/882431


Solution

  • Try this code

    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
    
    /* Get the HTML or whatever is linked in $url. */
    $response = curl_exec($handle);
    
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if($httpCode == 504) {
        /* Handle 504 here. */
    } else {
        /* Process data */
    }
    
    curl_close($handle);