json使用php解析时输出错误

时间:2018-01-22 09:38:31

标签: php json

我有问题。有些微控制器作为网络服务器工作,并输出有关温度和湿度的信息(传感器连接到此MC)作为JSON数据。我写了一个php解析脚本,它只连接到这个网页(每个MC有4个传感器,总共有8个MC,所以有8个页面,每个有4个传感器),并显示8个MC的所有32个传感器作为1个表。但问题是,我不明白如何写“如果错误 - 显示”传感器不可用“。

这是1 MC的网页示例。

[{ "Sensor":"T1.1","temperature":22.90,"humidity":14.30},{ "Sensor":"T1.2","temperature":23.60,"humidity":14.70},{ "Sensor":"T1.3","temperature":22.40,"humidity":16.90},{ "Sensor":"T1.4","temperature":23.50,"humidity":17.10}]

这是一个输出的PHP脚本

$url = 'http://192.168.40.61/'; 
        $data = file_get_contents($url);
        $characters = json_decode($data); 

        $url2 = 'http://192.168.40.62/'; 
        $data2 = file_get_contents($url2); 
        $characters2 = json_decode($data2); 

        $url3 = 'http://192.168.40.63/'; 
        $data3 = file_get_contents($url3); 
        $characters3 = json_decode($data3);

        $url4 = 'http://192.168.40.64/'; 
        $data4 = file_get_contents($url4);
        $characters4 = json_decode($data4);

        $url7 = 'http://192.168.40.65/'; 
        $data7 = file_get_contents($url7);
        $characters7 = json_decode($data7);
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Monitoring</title>
  <style type="text/css">
   body
   {
    background-color: white; 
    color: black;
   } 
    header { 
    background: white url(img/header.png) repeat-x; 
   }
    table {
     border-collapse: collapse;
     border: 5px double #000;
     border-color: black;
    }
    th {
     background: gray; 
     text-align: center; 
     color: RGB(255, 241, 213);
    }
    td, th {
     border: 1px solid #800; 
     padding: 8px; 
     border-color: black;
    } 
    .m {
        float:left;
        margin-left: 15px;
    }
  </style>
<body>
<header>
<div class="header-bg">
     <center><img src="/img/header.png" alt="EEEEE"></center>
</div>
</header>
<div class="m">
 <table width="30%" cellspacing="0" border="1">
    <tbody>
        <tr>
            <td colspan="3">
                <center><b>MC №1</b></center>
            </td>
        </tr>
            <th>Sensor number</th>
            <th>Temp С &deg; </th>
            <th>Hum %</th>
        </tr>
            <?php   
            foreach ($characters as $character) {
                echo '<tr>';
                echo '<td align="center">' . $character->Sensor . '</td></center>';
                echo '<td align="center">' . $character->temperature . '</td>';
                echo '<td align="center">' . $character->humidity . '</td>';
                echo '</tr>';
            }
            ?>
    </tbody>
</table>
</div>
<div class="m">
<table width="30%" cellspacing="0" border="1">
    <tbody>
        <tr>
            <td colspan="3">
                <center><b>MC №2</b></center>
            </td>
        </tr>
            <th>Sensor numver</th>
            <th>Temp С &deg; </th>
            <th>Hum %</th>
        </tr>
            <?php   
            foreach ($characters2 as $character2) {
                echo '<tr>';
                echo '<td align="center">' . $character2->Sensor . '</td></center>';
                echo '<td align="center">' . $character2->temperature . '</td>';
                echo '<td align="center">' . $character2->humidity . '</td>';
                echo '</tr>';
            }
            ?>
    </tbody>
</table>
</div>
<div class="m">
<table width="30%" cellspacing="0" border="1">
    <tbody>
        <tr>
            <td colspan="3">
                <center><b>MC №3</b></center>
            </td>
        </tr>
            <th>Sensor number</th>
            <th>Temp С &deg; </th>
            <th>Hum %</th>
        </tr>
            <?php   
            foreach ($characters3 as $character3) {
                echo '<tr>';
                echo '<td align="center">' . $character3->Sensor . '</td></center>';
                echo '<td align="center">' . $character3->temperature . '</td>';
                echo '<td align="center">' . $character3->humidity . '</td>';
                echo '</tr>';
            }
            ?>
    </tbody>
</table>
</div>
<div class="m">
<table width="30%" cellspacing="0" border="1">
    <tbody>
        <tr>
            <td colspan="3">
                <center><b>MC №4</b></center>
            </td>
        </tr>
            <th>Sensor Number</th>
            <th>Temp С &deg; </th>
            <th>Hum %</th>
        </tr>
            <?php   
            foreach ($characters4 as $character4) {
                echo '<tr>';
                echo '<td align="center">' . $character4->Sensor . '</td></center>';
                echo '<td align="center">' . $character4->temperature . '</td>';
                echo '<td align="center">' . $character4->humidity . '</td>';
                echo '</tr>';
            }
            ?>
    </tbody>
</table>
</div>
<div class="m">
<table width="30%" cellspacing="0" border="1">
    <tbody>
        <tr>
            <td colspan="3">
                <center><b>MC №7</b></center>
            </td>
        </tr>
            <th>Sensor number</th>
            <th>Temp С &deg; </th>
            <th>Hum %</th>
        </tr>
            <?php   
            foreach ($characters7 as $character7) {
                echo '<tr>';
                echo '<td align="center">' . $character7->Sensor . '</td></center>';
                echo '<td align="center">' . $character7->temperature . '</td>';
                echo '<td align="center">' . $character7->humidity . '</td>';
                echo '</tr>';
            }
            ?>
    </tbody>
</table>
</div>
</body>
</html>

当其中一个MC实际上不可用时,只有一个空页面,没有错误指示。我怎样才能为每张桌子写出来?提前谢谢你。

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

  1. 最好将$字符放入数组中以便以后更容易输出,例如

    $urls = array("http://192.168.40.61/", "http://192.168.40.62/", "http://192.168.40.62/", "http://192.168.40.63/", "http://192.168.40.64/", "http://192.168.40.65/" );
    
    foreach($urls as $key => $url) {
        $data = file_get_contents($url);
        $characters[$key] = json_decode($data);
    }
    
  2. 在输出数据之前检查数据是否可用:可用:输出表格;不可用:发消息。

    foreach($characters as  $key => $character_this) {
    
        if(count($character_this) < 1) {
            echo 'Data for No. ' .$key. ' is not available';
        } else {
            $rows = show_table($character_this);  
        };}
    
    function  show_table($character_this){
    
        foreach ($character_this as $character) {
            $rows .= '<tr>';
            $rows .= '<td align="center">' . $character->Sensor . '</td></center>';
            $rows .= '<td align="center">' . $character->temperature . '</td>';
            $rows .= '<td align="center">' . $character->humidity . '</td>';
            $rows .= '</tr>';
        }
        return $rows;}
    
  3. 我不知道为什么我不能将最后一个}放在一个单独的行上。

    1. 将行放入每张表中。

答案 1 :(得分:0)

每次使用时,请尝试在file_get_contents()上抑制错误(使用@)。如果结果为=== false,您可以查看它是什么类型的错误:

$mc = '[THE-IP-ADDR]';
$data = @file_get_contents($mc);
if ($data === false) {
     $headers = get_headers($mc);
     $responseCode = substr($headers[0], 9, 3);
     // Handle errors based on response code
     if ($responseCode == '404') {
         //do something, page is missing
     }
     // Etc.
} else {
     // Do default json rendering here.
}

您甚至可以编写一个函数来获取错误,因为您将多次使用它:

function get_mc_error($mc) {
    $headers = get_headers($mc);
    $responseCode = substr($headers[0], 9, 3);
    // Handle errors based on response code
    if ($responseCode == '404') {
        return 'You\'re page is missing.
        // or return json_encode(array('error'=>true,'errorMsg'=>'Missing page'));
    }
    // Etc.
}

然后做:

if($data === false) {
    $errorMsg[] = get_mc_error($mc);
    // $data = get_mc_error($mc); JSON solution
}