cURL retry in PHP

bgcode picture bgcode · May 21, 2011 · Viewed 14.4k times · Source

Is it possible to set the option to retry on timeouts in PHP cURL?

I know I can do this by coding it to retry on failure, just wondering if there's some way to do it through an option.

Answer

Cyrec picture Cyrec · Apr 11, 2014

you can use the errno to retry like this:

$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$retry = 0;
while(curl_errno($curl) == 28 && $retry < 3){
    $response = curl_exec($curl);
    $retry++;
}

$error_codes=array(
    [1] => 'CURLE_UNSUPPORTED_PROTOCOL', 
    [2] => 'CURLE_FAILED_INIT', 
    [3] => 'CURLE_URL_MALFORMAT', 
    [4] => 'CURLE_URL_MALFORMAT_USER', 
    [5] => 'CURLE_COULDNT_RESOLVE_PROXY', 
    [6] => 'CURLE_COULDNT_RESOLVE_HOST', 
    [7] => 'CURLE_COULDNT_CONNECT', 
    [8] => 'CURLE_FTP_WEIRD_SERVER_REPLY',
    [9] => 'CURLE_REMOTE_ACCESS_DENIED',
    [11] => 'CURLE_FTP_WEIRD_PASS_REPLY',
    [13] => 'CURLE_FTP_WEIRD_PASV_REPLY',
    [14]=>'CURLE_FTP_WEIRD_227_FORMAT',
    [15] => 'CURLE_FTP_CANT_GET_HOST',
    [17] => 'CURLE_FTP_COULDNT_SET_TYPE',
    [18] => 'CURLE_PARTIAL_FILE',
    [19] => 'CURLE_FTP_COULDNT_RETR_FILE',
    [21] => 'CURLE_QUOTE_ERROR',
    [22] => 'CURLE_HTTP_RETURNED_ERROR',
    [23] => 'CURLE_WRITE_ERROR',
    [25] => 'CURLE_UPLOAD_FAILED',
    [26] => 'CURLE_READ_ERROR',
    [27] => 'CURLE_OUT_OF_MEMORY',
    [28] => 'CURLE_OPERATION_TIMEDOUT',
    ....