如何使用PHP从API返回的JSON获取特定值

时间:2018-10-01 16:29:02

标签: php json

我正在努力通过使用eia.gov提供的API检索更新的数据来创建HTML表。数据是每个州的电价,我想获取它们提供的最新月份(在本例中为7月,因为它们有2-3个月的滞后时间)以及上一年的当前值。例如,2018年7月为12.28,2017年7月为12.53。

我目前正在这样调用API:http://api.eia.gov/geoset/?geoset_id=ELEC.PRICE.RES.M&regions=USA-AL&api_key=xxx&out=json&start=2017&end=2018,但是您可以传递不同的开始和结束值。

最好让两个电话只得到一个日期,例如: http://api.eia.gov/geoset/?geoset_id=ELEC.PRICE.RES.M&regions=USA-AL&api_key=xxx&out=json&start=201807&end=20180http://api.eia.gov/geoset/?geoset_id=ELEC.PRICE.RES.M&regions=USA-AL&api_key=xxx&out=json&start=201707&end=201707

这是使用http://api.eia.gov/geoset/?geoset_id=ELEC.PRICE.RES.M&regions=USA-AL&api_key=xxx&out=json&start=2017&end=2018从API返回的完整JSON的样子:

{
    "geoset": {
        "geoset_id": "ELEC.PRICE.RES.M",
        "setname": "Average retail price of electricity : residential : monthly",
        "f": "M",
        "units": "cents per kilowatthour",
        "unitsshort": null,
        "series": {
            "USA-AL": {
                "series_id": "ELEC.PRICE.AL-RES.M",
                "name": "Average retail price of electricity : Alabama : residential : monthly",
                "region": "USA-AL",
                "latlon": null,
                "unitsshort": null,
                "start": "200101",
                "end": "201807",
                "data": [
                    ["201807", 12.28],
                    ["201806", 12.41],
                    ["201805", 12.49],
                    ["201804", 12.79],
                    ["201803", 12.65],
                    ["201802", 12.29],
                    ["201801", 11.59],
                    ["201712", 11.87],
                    ["201711", 12.54],
                    ["201710", 12.73],
                    ["201709", 13.03],
                    ["201708", 12.78],
                    ["201707", 12.53],
                    ["201706", 12.79],
                    ["201705", 12.73],
                    ["201704", 12.71],
                    ["201703", 12.81],
                    ["201702", 12.9],
                    ["201701", 12.09]
                ]
            }
        }
    }
}

我的PHP代码当前如下所示:

<?php

$service_url = "http://api.eia.gov/geoset/?geoset_id=ELEC.PRICE.RES.M&regions=USA-AL&api_key=xxx&out=json&start=2017&end=2018";

$get_data = callAPI('GET', $service_url, false);
$response = json_decode($get_data);

?>

<div class="container">

    <div class="row">
        <div class="col-md-12">
            <h1>Electricity Costs By State</h1>
            <p>Here we've compiled data to show you just how much energy costs can vary.</p>
        </div>
    </div>

    <hr />

    <div class="row">
        <div class="col-md-12">

        <table class="table">
  <thead>
    <tr>
      <th scope="col">State</th>
      <th scope="col">Average Electric Rate July 2018 </th>
      <th scope="col">Average Electric Rate July 2017</th>
    </tr>
  </thead>
  <tbody>

    <?php 

        foreach ($response->geoset->series as $series) {
            echo "<tr>";
            echo "<td>" . $series->name . "</td>";
                foreach ($series->data as $data) {
                    echo "<td>" . $value = $data[1] . "</td>";
                }
            echo "</tr>";
        }

    ?>

  </tbody>
</table>

0 个答案:

没有答案