Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,使用数组的下一个值重试数组循环中的语句_Php_Arrays_Loops - Fatal编程技术网

在php中,使用数组的下一个值重试数组循环中的语句

在php中,使用数组的下一个值重试数组循环中的语句,php,arrays,loops,Php,Arrays,Loops,我再次来到这里,学习了越来越多关于PHP的知识,但我的场景仍然存在一些问题,我的大多数场景都已编程并顺利解决,但我发现了一个问题,但要理解它,我需要首先解释一下: 我有一个PHP脚本,可以被任何客户机调用,它的工作是接收请求,从我手动定义的列表ping到代理,以了解代理是否可用,如果可用,我继续使用带有POST方法的“curl”检索响应。逻辑是这样的: $proxyList = array('192.168.3.41:8013'=> 0, '192.168.3.41:8023'=>0

我再次来到这里,学习了越来越多关于PHP的知识,但我的场景仍然存在一些问题,我的大多数场景都已编程并顺利解决,但我发现了一个问题,但要理解它,我需要首先解释一下:

我有一个PHP脚本,可以被任何客户机调用,它的工作是接收请求,从我手动定义的列表ping到代理,以了解代理是否可用,如果可用,我继续使用带有POST方法的“curl”检索响应。逻辑是这样的:

$proxyList = array('192.168.3.41:8013'=> 0, '192.168.3.41:8023'=>0, '192.168.3.41:8033'=>0);
$errorCounter = 0;

foreach ($proxyList as $key => $value){
 if(!isUrlAvailable($key){ //It means it is NOT available so I count errors
    $errorCounter++;
 } else { //It means it is AVAILABLE
    $result = callThisProxy($key);
 }
}
函数“isurlavable”使用$fsockopen来知道代理是否可用。如果没有,我将使用前面提到的
CURL
生成一个
POST
,该函数具有
callThisProxy()
类似于:

 $ch = curl_init($proxyUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,'xmlQuery='.$rawXml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $info = curl_exec ($ch);
    if($isDebug){echo 'Info in the moment: '.$info.'<br/>';}
    curl_close ($ch);
$result = "";

while ($result == "")
{
    foreach ($proxyList as $key => $value)
    {
        if (!isUrlAvailable($key))
        {
            $errorCounter++;
        }
        else
        {
            $result = callThisProxy($key);
        }
    }
}

// Now check $result, which should contain the first successful callThisProxy()
//   result, or nothing if none of the keys worked.
我测试了它,当我这样做时,$结果是空字符串
'
。但问题是我丢失了该请求,我的目标是使用下一个
$key
代理重试它。因此,我一直在考虑一个
“do”,而当我调用结果时,
。但不确定,如果它是好的,或者有更好的方法来做,所以请我在这个问题上寻求帮助。提前感谢您的时间,欢迎您的回复。谢谢。

可能是:

 $ch = curl_init($proxyUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,'xmlQuery='.$rawXml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $info = curl_exec ($ch);
    if($isDebug){echo 'Info in the moment: '.$info.'<br/>';}
    curl_close ($ch);
$result = "";

while ($result == "")
{
    foreach ($proxyList as $key => $value)
    {
        if (!isUrlAvailable($key))
        {
            $errorCounter++;
        }
        else
        {
            $result = callThisProxy($key);
        }
    }
}

// Now check $result, which should contain the first successful callThisProxy()
//   result, or nothing if none of the keys worked.

您可以保留一个仍然需要尝试的代理列表。当您遇到错误或得到有效响应时,您将从代理列表中删除代理以重试。如果您没有得到良好的响应,请将其保留在列表中,稍后再试

$proxiesToTry = $proxyList;
$i = 0;

while (count($proxiesToTry) != 0) {
    // reset to beginning of array
    if($i >= count($proxiesToTry)) 
        $i = 0;

    $proxy = $proxiesToTry[$i];

    if (!isUrlAvailable($proxy)) { //It means it is NOT available so I count errors
        $errorCounter++;
        unset($proxiesToTry[$i]);
    } else { //It means it is AVAILABLE
        $result = callThisProxy($proxy); 
        if($result != "") // If we got a response remove it from the array of proxies to try.
            unset($proxiesToTry[$i]);
    }

    $i++;
}

注意:如果您从未从某个代理收到有效响应,您将永远无法跳出此循环。

我很困惑,您在调用代理后不会退出循环,所以无论
callThisProxy($key)
的返回值是多少,它都不会尝试下一个代理吗?它将尝试下一个代理,但是关闭的代理的结果将为null或空,而目标是始终为其获取结果。