重试超时CURL

时间:2014-07-01 14:08:26

标签: php curl

如果超时,重试curl请求的更好方法是什么?

我使用邪恶的GOTO

提出了这个解决方案
retry:

$result = curlPost($ch, "something.php", $cookie, http_build_query($arg));

if (curl_errno($ch) == 28) {
    goto retry;
}

// Do something

curlPost()函数中,有

curl_setopt($curl, CURLOPT_TIMEOUT, 3);

1 个答案:

答案 0 :(得分:3)

你可以使用do-while循环。

$count = 0;
$max_tries = 5;
$success = true;
do {
    $result = curlPost($ch, "something.php", $cookie, http_build_query($arg));
    $count++;
    if($count >= $max_tries) {
        $success = false;
        break;
    }
}
while(curl_errno($ch) == 28);

if($success == false) {
    // If it got here it tried 5 times and still didn't get a result.
    // More code here for what you want to do...
}