如何使用 PHP 从 Wunderground 读取警报的 JSON 输出?

How do I use PHP to read the JSON output for ALERTS from Wunderground?

似乎从 Wunderground 读取 'conditions' JSON 输出与准备从 'alerts' 输出非常不同。下面是我的代码的简化版本。

$json_stringalert = file_get_contents("http://api.wunderground.com/api/myKey/alerts/q/MO/Kansas_City.json");
$parsed_jsonalert = json_decode($json_stringalert);
$description = $parsed_jsonalert->{'alerts'}->{'description'}; 
echo "${description}

回显是空白的,没有错误。 URL 的输出部分看起来像这样;

{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "alerts": 1
  }
    }
        ,"query_zone": "037",
    "alerts": [
        {
        "type": "FIR",
        "description": "Fire Weather Warning",
        "date": "10:27 am CST on March 2, 2017",
        "date_epoch": "1488472020",
        "expires": "6:00 PM CST on March 02, 2017",
        "expires_epoch": "1488499200",
        "tz_short":"CST",
        "tz_long":"America/Chicago",
        "message": "\u000A...Red flag warning in effect until 6 PM CST this evening for\u000Abreezy northwest winds and low relative humidity values for fire \u000Aweather zones 020, 021, 025, 028, 029, 030, 037, 038, 043, 044, \u000A045, 053, 054, 057, 060, 102, 103, 104, and 105...\u000A\u000AThe National Weather Service in Kansas City/Pleasant Hill has\u000Aissued a red flag warning, which is in effect until 6 PM CST this\u000Aevening. \u000A\u000A* Affected area...fire weather zones 025, 057, 060, 102, 103, \u000A 104, and 105.Fire weather zones 020, 021, 028, 029, 030, 037, \u000A 038, 043, 044, 045, 053, and 054. \u000A\u000A* Wind...sustained northwest winds of 20 mph with higher gusts are\u000A expected this afternoon. \u000A\u000A* Humidity...relative humidity values will fall into the low to\u000A middle 20 percent range. \u000A\u000A* Impacts...any fires that develop will likely spread rapidly. \u000A Outdoor burning is not recommended.\u000A\u000APrecautionary/preparedness actions...\u000A\u000AA red flag warning means that critical fire weather conditions\u000Aare either occurring now, or will shortly. A combination of\u000Astrong winds, low relative humidity, and warm temperatures can\u000Acontribute to extreme fire behavior.\u000A\u000A\u000A\u000ACramer\u000A\u000A\u000A",
        "phenomena": "FW",
        "significance": "W", ...
and more below.

有人可以帮我写代码来使用 'description' 和其他输出吗?

$alert_array = $parsed_jsonalert->alerts; 
foreach($alert_array as $alert) {
    echo $alert->description;
}

在您的 JSON 输出中,Alerts 键是一个数组,因此 $json->alerts->description 不会访问任何特定内容。我假设你想要这里的所有警报而不仅仅是一个特定的键($json->alerts[0]->description)所以你需要像这样循环你的数据:

function getAlerts($jsonString)
{
  $parsedObj = json_decode($jsonString);

  // This is the entire array of alert objects.
  $alertsArray = $parsedObj->alerts;

  // This next section is only necessary if you want to extract specific 
  // keys to be returned instead of the entire array of objects. If you
  // want everything than just return the $alertsArray variable.
  $alerts = array();

  foreach ($alertsArray as $alert) {
    // Add single key to alerts return array.
    // alternatively here is where you would build a filtered array
    // of values to return if you need more than one.
    $alerts[] = $alert->description;
  }
  return $alerts;
}
$file = file_get_contents("http://api.wunderground.com/api/myKey/alerts/q/MO/Kansas_City.json");
$alerts = getAlerts($file);
echo implode("\n", $alerts);