如果超时,重试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);
答案 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...
}