需要帮助 有5台服务器
$server1 = “1234”;
$server2 = “”;
$server3 = “5463”;
$server4 = “”;
$server5 = “1827”;
$server = array($server1, $server2, $server3, $server4, $server5);
然后初始化curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://api.domain.com/$server1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$json = json_decode($result, true);
$ip = $json[droplet][networks][v4][0][ip_address];
$status = $json[droplet][status];
$currentapp = $json[droplet][image][name];
$hostname = $json[droplet][name];
$memory = $json[droplet][memory];
$cpu = $json[droplet][vcpus];
$disk = $json[droplet][disk];
然后结果在表格中回显:
<tr>
<td style="text-align:center"><?php echo $ip; ?></td>
<td style="text-align:center"><?php echo $status; ?></td>
<td style="text-align:center"><?php echo $currentapp; ?></td>
<td style="text-align:center"><?php echo $hostname; ?></td>
<td style="text-align:center"><?php echo $memory; ?></td>
<td style="text-align:center"><?php echo $cpu; ?></td>
<td style="text-align:center"><?php echo $disk; ?></td>
</tr>
现在请在这里帮助我 如何在不同的行中显示不同服务器的数据。 如果服务器2和4为空,那么只有服务器1 3和5应该以3行显示。
答案 0 :(得分:0)
首先将所有服务器值放入数组中。像这样:
$server1 = “1234”;
$server2 = “”;
$server3 = “5463”;
$server4 = “”;
$server5 = “1827”;
$serverArray = array(
$server1,
$server2,
$server3,
$server4,
$server5,
);
$infoArray = NULL; // To store all the information
$counter = 0;
foreach($serverArray as $server){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://api.domain.com/$server1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$json = json_decode($result, true);
$ip = $json[droplet][networks][v4][0][ip_address];
$status = $json[droplet][status];
$currentapp = $json[droplet][image][name];
$hostname = $json[droplet][name];
$memory = $json[droplet][memory];
$cpu = $json[droplet][vcpus];
$disk = $json[droplet][disk];
$infoArray[$counter]['ip'] = $ip;
$infoArray[$counter]['status'] = $status;
$infoArray[$counter]['currentapp'] = $currentapp;
$infoArray[$counter]['hostname'] = $hostname;
$infoArray[$counter]['memory'] = $memory;
$infoArray[$counter]['cpu'] = $cpu;
$infoArray[$counter]['disk'] = $disk;
$counter++;
}
对于视图文件
<?php
$output = '';
foreach($infoArray as $info){
$output += "<tr>";
$output += "<td style='text-align:center'>{$info['ip']}</td>";
$output += "<td style='text-align:center'>{$info['status']}</td>";
$output += "<td style='text-align:center'>{$info['currentapp']}</td>";
$output += "<td style='text-align:center'>{$info['hostname']}</td>";
$output += "<td style='text-align:center'>{$info['memory']}</td>";
$output += "<td style='text-align:center'>{$info['cpu']}</td>";
$output += "<td style='text-align:center'>{$info['disk']}</td>";
$output += "</tr>";
}
echo $output;
&GT;
答案 1 :(得分:0)
实现它的一种方法是将服务器视为一个数组。然后用户foreach迭代。
$serverArray = array();
$serverArray[] = '1234';
$serverArray[] = '';
$serverArray[] = '5463';
$serverArray[] = '';
$serverArray[] = '1827';
foreach( $serverArray as $serverValue )
{
if( $serverValue <> '' )
{
$serverURL = 'https://api.domain.com/' .$serverValue;
// cURL code here
// Your table display code here
}
}
答案 2 :(得分:0)
Finanlly这是有效的
<html>
<?php
$serverArray = array();
$serverArray[] = '2720973';
$serverArray[] = '277543';
$serverArray[] = '2720153';
$serverArray[] = '';
$serverArray[] = '';
foreach( $serverArray as $serverValue )
{
if( $serverValue <> '' )
{
// initialize curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://api.domain.com/$serverValue");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
//decodes json and prints id
$json = json_decode($result, true);
$ip = $json[droplet][networks][v4][0][ip_address];
$status = $json[droplet][status];
$currentapp = $json[droplet][image][name];
$hostname = $json[droplet][name];
$memory = $json[droplet][memory];
$cpu = $json[droplet][vcpus];
$disk = $json[droplet][disk];
?>
<table>
<tr>
<td style="text-align:center"><?php echo $ip; ?></td>
<td style="text-align:center"><?php echo $status; ?></td>
<td style="text-align:center"><?php echo $currentapp; ?></td>
<td style="text-align:center"><?php echo $hostname; ?></td>
<td style="text-align:center"><?php echo $memory; ?></td>
<td style="text-align:center"><?php echo $cpu; ?></td>
<td style="text-align:center"><?php echo $disk; ?></td>
<?php }
} ?>