在特定条件下重做最后一个 foreach 循环命令
Redo last foreach loop command on specific condition
我们正在向网络服务发出 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 ) {
//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 {
$retriesLeft = 3;
do {
sleep(30);
// perform the request again
--$retriesLeft;
}
while (!isset($response) && $retriesLeft);
}
}
}
更新:简化版
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;
}
我们正在向网络服务发出 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 ) {
//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 {
$retriesLeft = 3;
do {
sleep(30);
// perform the request again
--$retriesLeft;
}
while (!isset($response) && $retriesLeft);
}
}
}
更新:简化版
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;
}