【问题标题】:Get JSON from two URLs and echo data from each (PHP)从两个 URL 获取 JSON 并从每个 URL 回显数据 (PHP)
【发布时间】:2018-03-05 19:00:04
【问题描述】:

我有多个 JSON API URL 正在尝试从中提取数据。它们的结构都相同,但数据来自不同的位置。我正在尝试获取每个文件并在表格中回显来自它们的数据。当我分别从每个文件中提取数据并为每个 TR 重复代码时,我已经设法让它工作,但我试图让它循环,就像从 MySQL 中提取数据行一样数据库,因此它会在前端的表格中单独显示每个位置的数据。

JSON 文件结构


 {
      "Altimeter": "3005",      
      "Flight-Rules": "VFR", 
      "Raw-Report": "KJFK 241851Z 19009KT 10SM CLR 31/18 A3005 RMK AO2 SLP174 T03060183", 
      "Station": "KJFK", 
      "Visibility": "10", 
      "Wind-Direction": "190", 
      "Wind-Gust": "", 
      "Wind-Speed": "09", 
  }

我已经通过下面的代码设法让它在一个站点上工作:

<?
$json = file_get_contents('Station_1_URL');
$decoded = json_decode($json, true);
?>

<table>
    <tbody>
        <tr>
            <td><? echo $decoded['Station']; ?></td>
            <td><? echo $decoded['Flight-Rules']; ?></td>
            <td><? echo $decoded['Wind-Direction']; ?> @ <? echo $decoded['Wind-Speed']; ?></td>
            <td><? echo $decoded['Altimeter']; ?></td>
        </tr>
    </tbody>
</table>

我希望它从两个站收集数据并输出如下

<table>
        <tbody>
            <tr> <!-- This is data from the first station -->
                <td><? echo $decoded['Station']; ?></td>
                <td><? echo $decoded['Flight-Rules']; ?></td>
                <td><? echo $decoded['Wind-Direction']; ?> @ <? echo $decoded['Wind-Speed']; ?></td>
                <td><? echo $decoded['Altimeter']; ?></td>
            </tr>

            <tr> <!-- This is data from the next station -->
                <td><? echo $decoded['Station']; ?></td>
                <td><? echo $decoded['Flight-Rules']; ?></td>
                <td><? echo $decoded['Wind-Direction']; ?> @ <? echo $decoded['Wind-Speed']; ?></td>
                <td><? echo $decoded['Altimeter']; ?></td>
            </tr>
        </tbody>
    </table>

【问题讨论】:

    标签: php json


    【解决方案1】:

    创建一个您需要遍历的所有站点 URL 的列表,并将前端包装在 foreach 循环中:

    <?php
    $station_urls = ['Station_1_URL', 'Station_2_URL', 'Station_3_URL'];
    ?>
    <table>
        <tbody>
        <? foreach ($station_urls as $station_url) :
            $json = file_get_contents($station_url);
            $decoded = json_decode($json, true);
        ?>
        <tr>
            <td><? echo $decoded['Station']; ?></td>
            <td><? echo $decoded['Flight-Rules']; ?></td>
            <td><? echo $decoded['Wind-Direction']; ?> @ <? echo $decoded['Wind-Speed']; ?></td>
            <td><? echo $decoded['Altimeter']; ?></td>
        </tr>
        <? endforeach; ?>
        </tbody>
    </table>
    

    【讨论】:

    • 完美运行!谢谢!
    【解决方案2】:

    将您的电台网址放在一个数组中

    <?php
      $stationURLs = array(Station_1_URL, Station_2_URL, ...);
      foreach ($stationURLs as $stationURL) {
        // fetch the JSON response
        // print the <tr>
        // (same as your chunk of code for station1)
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-08-06
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      相关资源
      最近更新 更多