2014-07-01 38 views
1

如果发生超时,重试卷曲请求的更好方法是什么?重试超时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

使用循环并试图反击 – Phantom

+0

@Phantom我将有一个脚本多个卷曲的要求,为每个卷曲要求可能有点过分做循环?如果在任何卷曲请求中有超时,我想从头开始重新开始。 – user1246800

+2

你为什么使用'goto'? – silkfire

回答

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... 
} 
+1

注意:'$ success = false';是一个无法访问的声明 – snowdragon

+0

@snowdragon:重新排序! :-) –

+0

实际上,如果第五次重试成功,也会说失败。 :-) –