重试超时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);

你可以使用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...
}

最新更新