我不知道怎么做。有一个XML Api服务器,我用cURL获取内容;它工作得很好。现在我要调用creditCardPreprocessors
状态。它也有"正在进行状态",PHP应该等待直到进程完成。我已经用睡觉和其他方法试过了,但我做不到。这是我尝试的一个简化的例子:
function process_state($xml){
if($result = request($xml)){
// It'll return NULL on bad state for example
return $result;
}
sleep(3);
process_state($xml);
}
我知道,这可能是一个无限循环,但我已经尝试添加计数退出,如果它达到5;它不会退出,服务器会挂起,我将有500分钟的错误,Apache无法访问该vhost。
编辑:另一个例子$i = 0;
$card_state = false;
// We're gona assume now the request() turns back NULL if card state is processing TRUE if it's done
while(!$card_state && $i < 10){
$i++;
if($result = request('XML STUFF')){
$card_state = $result;
break;
}
sleep(2);
}
您定义的递归方法可能会导致问题,这取决于您从服务器返回的响应时间。我想你会想在这里使用一个while循环。它使请求保持序列化。
$returnable_responses = array('code1','code2','code3'); // the array of responses that you want the function to stop after receiving
$max_number_of_calls = 5; // or some number
$iterator = 0;
$result = NULL;
while(!in_array($result,$returnable_responses) && ($iterator < $max_number_of_calls)) {
$result = request($xml);
$iterator++;
}