Php 在特定条件下重做最后一个foreach循环命令

Php 在特定条件下重做最后一个foreach循环命令,php,web-services,loops,foreach,error-handling,Php,Web Services,Loops,Foreach,Error Handling,我们向Web服务发出了1000次请求。每次发出请求时,我们都希望得到响应,但有时Web服务会失败并且不提供响应。当这种情况发生时,我们希望睡眠30秒,然后在最后一次失败时重新运行该循环。如何做到这一点 以下是基本结构: foreach ( $objects as $object ) { foreach ( $otherObjects as $otherObject ) { //Prepare request data from $object and $otherObj

我们向Web服务发出了1000次请求。每次发出请求时,我们都希望得到响应,但有时Web服务会失败并且不提供响应。当这种情况发生时,我们希望睡眠30秒,然后在最后一次失败时重新运行该循环。如何做到这一点

以下是基本结构:

foreach ( $objects as $object ) {
    foreach ( $otherObjects as $otherObject ) {

        //Prepare request data from $object and $otherObjects

        //Send request to webservice using cURL

        //Receive response and check to see if success
        if ( isset( $response ) ) {
            //Save response to database
        } else {
            //Not successful. Sleep for 30 seconds. Send request again up to 3 times. If ultimately successful continue, if not break and alert system admin.
        }
    }
}
更新:简化版
您可以将请求分解为一个函数:

foreach ( $objects as $object ) {
    foreach ( $otherObjects as $otherObject ) {
        $tries = 0;
        //Receive response and check to see if success.
        while( ($response = doRequest($object, $otherObject)) === false && $tries < 3) {
            //Not successful. Sleep for 30 seconds. Send request again up to 3 times.
            $tries++;
            sleep(30);
        }
        if ( $response ) {
            //Successful save response to database.
        } else {
            //Not successful break and alert system admin.
    }
}

function doRequest($object, $otherObject) {
    //Prepare request data from $object and $otherObject.
    //Send request to webservice using cURL.

    return $result;
}
foreach($objects作为$object){
foreach($otherObjects作为$otherObject){
$trys=0;
//接收响应并检查是否成功。
而($response=doRequest($object,$otherObject))==false&&$trys<3){
//未成功。睡眠30秒。再次发送请求最多3次。
$T++;
睡眠(30);
}
如果($答复){
//成功将响应保存到数据库。
}否则{
//未成功中断并通知系统管理员。
}
}
函数doRequest($object,$otherObject){
//准备来自$object和$otherObject的请求数据。
//使用cURL向webservice发送请求。
返回$result;
}

但是我如何重新运行失败的webservice请求?基本上,返回并重做内存中的最后一个循环,然后继续?只是更新了如何执行此操作。如果您需要更多信息,您可以提供更多关于您正在尝试执行的操作的信息,因为我们无法继续。Thx,这很有帮助。您是否可以简单地存储请求数据在$failedRequests中,然后在1000个请求结束时,循环通过$failedRequests并重试?我会将请求数据存储在$retriesLeft变量下。是否希望在继续执行其他请求时,在重试请求之前至少30秒?是的,在重试之前等待30秒,但不需要继续,直到tBonner的回答也很好,但我选择这个答案是因为逻辑更流畅,更直观易懂。
foreach ( $objects as $object ) {
    foreach ( $otherObjects as $otherObject ) {
        $retriesLeft = 4;

        //Prepare request data from $object and $otherObjects

        do {
            //Send request to webservice using cURL

            //Receive response and check to see if success

            if (isset($response)) {
                break;
            }
            else {
                sleep(30);
                --$retriesLeft;
            }
        }
        while (!isset($response) && $retriesLeft);
    }
}
foreach ( $objects as $object ) {
    foreach ( $otherObjects as $otherObject ) {
        $tries = 0;
        //Receive response and check to see if success.
        while( ($response = doRequest($object, $otherObject)) === false && $tries < 3) {
            //Not successful. Sleep for 30 seconds. Send request again up to 3 times.
            $tries++;
            sleep(30);
        }
        if ( $response ) {
            //Successful save response to database.
        } else {
            //Not successful break and alert system admin.
    }
}

function doRequest($object, $otherObject) {
    //Prepare request data from $object and $otherObject.
    //Send request to webservice using cURL.

    return $result;
}