问题描述
我不知道如何做到这一点。
有一个XML Api服务器,我得到的内容与cURL;它工作正常。现在我必须调用 creditCardPreprocessors
状态。它也有进行中状态,PHP应该等待进度完成。我已经用睡眠和其他方式,但我不能做到。这是我尝试的一个简化示例变体:
I don't know how to make this.There is an XML Api server and I'm getting contents with cURL; it works fine. Now I have to call the creditCardPreprocessors
state. It has 'in progress state' too and PHP should wait until the progess is finished. I tried already with sleep and other ways, but I can't make it. This is a simplified example variation of what I tried:
function process_state($xml){
if($result = request($xml)){
// It'll return NULL on bad state for example
return $result;
}
sleep(3);
process_state($xml);
}
我知道,这可能是一个有限循环,但我试图添加如果达到五则计数退出;它不会退出,服务器将挂断,我将有分钟的500错误,Apache不能访问该vhost。
I know, this can be an infite loop but I've tried to add counting to exit if it reaches five; it won't exit, the server will hang up and I'll have 500 errors for minutes and Apache goes unreachable for that 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循环。它保持请求序列化。
The recursive method you've defined could cause problems depending on the response timing you get back from the server. I think you'd want to use a while loop here. It keeps the requests serialized.
$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++;
}
这篇关于PHP等待使用cURL的正确响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!