Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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 当旋度返回200时停止foreach循环_Php_Api_Curl_Proxmox - Fatal编程技术网

Php 当旋度返回200时停止foreach循环

Php 当旋度返回200时停止foreach循环,php,api,curl,proxmox,Php,Api,Curl,Proxmox,我尝试从API与包含3台服务器的Proxmox集群通信。目标是在出现故障时至少连接到其中一个服务器,然后执行其他代码(此处不可见) 使用下面的代码,我使用“foreach”逐个连接到这些服务器,我希望当一个服务器从Curl返回“200”时,这个循环停止(这样我就可以继续使用这个在线服务器)。为了测试,我停止了第一台服务器,让其他2台服务器联机,但“foreach”循环保持连接第三台服务器 有什么想法吗?谢谢你,对不起我的英语 <?php $datas = array( array

我尝试从API与包含3台服务器的Proxmox集群通信。目标是在出现故障时至少连接到其中一个服务器,然后执行其他代码(此处不可见)

使用下面的代码,我使用“foreach”逐个连接到这些服务器,我希望当一个服务器从Curl返回“200”时,这个循环停止(这样我就可以继续使用这个在线服务器)。为了测试,我停止了第一台服务器,让其他2台服务器联机,但“foreach”循环保持连接第三台服务器

有什么想法吗?谢谢你,对不起我的英语

<?php

$datas = array(
    array(
        "apiurl" => "192.168.1.34:8006",
        "node" => "pve1",
        "user" => "root",
        "userpass" => "pass",
    ),
    array(
        "apiurl" => "192.168.1.35:8006",
        "node" => "pve2",
        "user" => "root",
        "userpass" => "pass",
    ),
    array(
        "apiurl" => "192.168.1.36:8006",
        "node" => "pve3",
        "user" => "root",
        "userpass" => "pass",
    )
);

do {
    foreach ($datas as $data) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://'.$data['apiurl'].'/api2/json/access/ticket');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$data['user'].'@pam&password='.$data['userpass']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);

        $headers = array();
        $headers[] = 'Content-Type: application/x-www-form-urlencoded';

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Server ' .$data['node']. ' is not reachable (error : ' .curl_error($ch). ')<br>';
        }
        else {
            $myArray = json_decode($result, true);
            $cookie = $myArray['data']['ticket'];
            $info = curl_getinfo($ch);
            echo "Server " .$data['node']. " is reachable (code is : " .$info['http_code']. ")<br>";
        }
    curl_close($ch);
    }
} while ($info['http_code'] !== 200);
?>

您可以通过向
break
提供参数来中断外部循环<代码>中断2表示中断第二个包含循环

while (true) {
    foreach ($datas as $data) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://'.$data['apiurl'].'/api2/json/access/ticket');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$data['user'].'@pam&password='.$data['userpass']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);

        $headers = array();
        $headers[] = 'Content-Type: application/x-www-form-urlencoded';

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Server ' .$data['node']. ' is not reachable (error : ' .curl_error($ch). ')<br>';
        }
        else {
            $myArray = json_decode($result, true);
            $cookie = $myArray['data']['ticket'];
            $info = curl_getinfo($ch);
            echo "Server " .$data['node']. " is reachable (code is : " .$info['http_code']. ")<br>";
            if ($info['http_code'] == 200) {
                break 2;
            }
        }
        curl_close($ch);
    }
}
while(true){
foreach($datas作为$data){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'https://'.$data['apirl'.]./api2/json/access/ticket');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,'username='.$data['user'.@pam&password='.$data['userpass']);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_超时,2);
$headers=array();
$headers[]='内容类型:application/x-www-form-urlencoded';
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$result=curl\u exec($ch);
if(旋度误差($ch)){
回显“服务器”。$data['node']”不可访问(错误:'.curl_error($ch)。
; } 否则{ $myArray=json_decode($result,true); $cookie=$myArray['data']['ticket']; $info=curl\u getinfo($ch); echo“服务器”。$data['node']”是可访问的(代码为:“..info['http_code'])”
”; 如果($info['http_code']==200){ 破口2; } } 卷曲关闭($ch); } }
这里有一个无限循环,因为正如您所说的,您已经知道阵列中的最后两个服务器不会成功响应,但是您的
foreach
循环将尝试所有3个,无论成功与否。因此,当您到达
foreach
循环的第三次迭代时,您会得到一个失败的响应,然后您到达
循环的末尾,而
循环确定条件仍然是
真的
,并继续。为了防止这种情况,只要在确定成功后从
foreach
循环中断即可。因为只有那么
循环的条件将变为
false
。如果愿意,还可以中断两个循环(使用
break 2
)。任何一个都是正确的。唯一的区别是,如果您希望outter(
while
)循环中的任何剩余逻辑发生,而不管成功与否

foreach ($datas as $data) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://'.$data['apiurl'].'/api2/json/access/ticket');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$data['user'].'@pam&password='.$data['userpass']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);

        $headers = array();
        $headers[] = 'Content-Type: application/x-www-form-urlencoded';

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Server ' .$data['node']. ' is not reachable (error : ' .curl_error($ch). ')<br>';
        }
        else {
            $myArray = json_decode($result, true);
            $cookie = $myArray['data']['ticket'];
            $info = curl_getinfo($ch);
            echo "Server " .$data['node']. " is reachable (code is : " .$info['http_code']. ")<br>";
            if (!empty($info['http_code']) && $info['http_code'] == 200) {
                break; // break out of either or both loops since we got a valid response
            }
        }
    curl_close($ch);
}
foreach($data作为$data){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'https://'.$data['apirl'.]./api2/json/access/ticket');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,'username='.$data['user'.@pam&password='.$data['userpass']);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_超时,2);
$headers=array();
$headers[]='内容类型:application/x-www-form-urlencoded';
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$result=curl\u exec($ch);
if(旋度误差($ch)){
回显“服务器”。$data['node']”不可访问(错误:'.curl_error($ch)。
; } 否则{ $myArray=json_decode($result,true); $cookie=$myArray['data']['ticket']; $info=curl\u getinfo($ch); echo“服务器”。$data['node']”是可访问的(代码为:“..info['http_code'])”
”; 如果(!empty($info['http\u code'])和&$info['http\u code']==200){ break;//由于得到了有效响应,因此中断其中一个或两个循环 } } 卷曲关闭($ch); }
什么
foreach
循环?这里有一个
while
循环
foreach
只遍历数组。这个循环显然是无限的。我以为“do while”会用“while($info['http_code']!==200);”停止“foreach”循环。但我错了,我不知道如何解决我的问题。它不会的。
foreach
循环在到达
while
循环之前首先遍历整个数组。由于
while
条件在我们通过
foreach
循环时总是
false
,因此
while
循环将永远持续。为了纠正我之前的评论,我的意思是说
while
条件总是
true
,显然不是
false
,因为
false
会导致它停止。好的,我明白了。。。谢谢。您不需要中断2,顺便说一句,
break
一个人就可以了。尤其是当您期望
while
循环中的任何逻辑发生时,无论成功与否。在这种情况下没有错误,因为没有,但也没有必要。
break
将只中断
foreach
循环,但是
while
循环将继续。不,while循环将停止,因为条件将变为
false
,因为我们现在有一个
$info['http\u code']==200
。True,但是为什么要检查相同的条件两次呢?请阅读我之前的评论。或者看看我的答案,看更详细的解释。