【问题标题】:Displaying JSON Weather API Response in PHP在 PHP 中显示 JSON 天气 API 响应
【发布时间】:2019-02-05 13:25:25
【问题描述】:

在这里查看了这么多其他问题后,我仍然坚持这一点。首先,我以前从未使用过 JSON 数据,我发现什么应该是相当简单却相当棘手。

我有来自 DarkSky Weather API 的类似 JSON 响应

{
    "latitude": -26.202271,
    "longitude": 28.043631,
    "timezone": "Africa\/Johannesburg",
    "daily": {
        "summary": "Light rain throughout the week, with high temperatures peaking at 28\u00b0C on Saturday.",
        "icon": "rain",
        "data": [
            {
                "time": 1549317600,
                "summary": "Mostly cloudy throughout the day.",
                "icon": "partly-cloudy-day",
                "sunriseTime": 1549338410,
                "sunsetTime": 1549385960,
                "moonPhase": 0.02,
                "precipIntensity": 0.0737,
                "precipIntensityMax": 0.6121,
                "precipIntensityMaxTime": 1549368000,
                "precipProbability": 0.14,
                "precipType": "rain",
                "temperatureHigh": 22.68,
                "temperatureHighTime": 1549364400,
                "temperatureLow": 15.14,
                "temperatureLowTime": 1549422000,
                "apparentTemperatureHigh": 22.68,
                "apparentTemperatureHighTime": 1549364400,
                "apparentTemperatureLow": 15.14,
                "apparentTemperatureLowTime": 1549422000,
                "dewPoint": 13.85,
                "humidity": 0.74,
                "pressure": 1015.08,
                "windSpeed": 1.84,
                "windGust": 7.01,
                "windGustTime": 1549342800,
                "windBearing": 40,
                "cloudCover": 0.73,
                "uvIndex": 8,
                "uvIndexTime": 1549357200,
                "visibility": 14.03,
                "ozone": 263.92,
                "temperatureMin": 15.02,
                "temperatureMinTime": 1549339200,
                "temperatureMax": 22.68,
                "temperatureMaxTime": 1549364400,
                "apparentTemperatureMin": 15.02,
                "apparentTemperatureMinTime": 1549339200,
                "apparentTemperatureMax": 22.68,
                "apparentTemperatureMaxTime": 1549364400
            },
            {
                "time": 1549404000,
                "summary": "Mostly cloudy throughout the day.",
                "icon": "partly-cloudy-day",
                "sunriseTime": 1549424856,
                "sunsetTime": 1549472325,
                "moonPhase": 0.05,
                "precipIntensity": 0.0432,
                "precipIntensityMax": 0.2819,
                "precipIntensityMaxTime": 1549454400,
                "precipProbability": 0.13,
                "precipType": "rain",
                "temperatureHigh": 24.32,
                "temperatureHighTime": 1549454400,
                "temperatureLow": 15.83,
                "temperatureLowTime": 1549508400,
                "apparentTemperatureHigh": 24.32,
                "apparentTemperatureHighTime": 1549454400,
                "apparentTemperatureLow": 15.83,
                "apparentTemperatureLowTime": 1549508400,
                "dewPoint": 13.06,
                "humidity": 0.67,
                "pressure": 1013.86,
                "windSpeed": 1.34,
                "windGust": 6.01,
                "windGustTime": 1549414800,
                "windBearing": 341,
                "cloudCover": 0.71,
                "uvIndex": 8,
                "uvIndexTime": 1549443600,
                "visibility": 16.09,
                "ozone": 258.39,
                "temperatureMin": 15.14,
                "temperatureMinTime": 1549422000,
                "temperatureMax": 24.32,
                "temperatureMaxTime": 1549454400,
                "apparentTemperatureMin": 15.14,
                "apparentTemperatureMinTime": 1549422000,
                "apparentTemperatureMax": 24.32,
                "apparentTemperatureMaxTime": 1549454400
            },
            {
                "time": 1549490400,
                "summary": "Light rain overnight.",
                "icon": "rain",
                "sunriseTime": 1549511300,
                "sunsetTime": 1549558690,
                "moonPhase": 0.08,
                "precipIntensity": 0.16,
                "precipIntensityMax": 0.6096,
                "precipIntensityMaxTime": 1549573200,
                "precipProbability": 0.23,
                "precipType": "rain",
                "temperatureHigh": 25.43,
                "temperatureHighTime": 1549540800,
                "temperatureLow": 16.47,
                "temperatureLowTime": 1549591200,
                "apparentTemperatureHigh": 25.43,
                "apparentTemperatureHighTime": 1549540800,
                "apparentTemperatureLow": 16.61,
                "apparentTemperatureLowTime": 1549591200,
                "dewPoint": 14.17,
                "humidity": 0.69,
                "pressure": 1013.06,
                "windSpeed": 2.28,
                "windGust": 7.57,
                "windGustTime": 1549497600,
                "windBearing": 342,
                "cloudCover": 0.52,
                "uvIndex": 12,
                "uvIndexTime": 1549533600,
                "visibility": 16.09,
                "ozone": 251.95,
                "temperatureMin": 15.83,
                "temperatureMinTime": 1549508400,
                "temperatureMax": 25.43,
                "temperatureMaxTime": 1549540800,
                "apparentTemperatureMin": 15.83,
                "apparentTemperatureMinTime": 1549508400,
                "apparentTemperatureMax": 25.43,
                "apparentTemperatureMaxTime": 1549540800
            },
            {
                "time": 1549576800,
                "summary": "Mostly cloudy throughout the day.",
                "icon": "partly-cloudy-day",
                "sunriseTime": 1549597745,
                "sunsetTime": 1549645053,
                "moonPhase": 0.11,
                "precipIntensity": 0.2819,
                "precipIntensityMax": 1.1481,
                "precipIntensityMaxTime": 1549584000,
                "precipProbability": 0.39,
                "precipType": "rain",
                "temperatureHigh": 25.17,
                "temperatureHighTime": 1549627200,
                "temperatureLow": 16.64,
                "temperatureLowTime": 1549677600,
                "apparentTemperatureHigh": 25.19,
                "apparentTemperatureHighTime": 1549627200,
                "apparentTemperatureLow": 16.64,
                "apparentTemperatureLowTime": 1549677600,
                "dewPoint": 15.46,
                "humidity": 0.77,
                "pressure": 1013.3,
                "windSpeed": 1.21,
                "windGust": 5.91,
                "windGustTime": 1549580400,
                "windBearing": 310,
                "cloudCover": 0.91,
                "uvIndex": 7,
                "uvIndexTime": 1549616400,
                "visibility": 16.09,
                "ozone": 249.44,
                "temperatureMin": 16.47,
                "temperatureMinTime": 1549591200,
                "temperatureMax": 25.17,
                "temperatureMaxTime": 1549627200,
                "apparentTemperatureMin": 16.61,
                "apparentTemperatureMinTime": 1549591200,
                "apparentTemperatureMax": 25.19,
                "apparentTemperatureMaxTime": 1549627200
            },
            {
                "time": 1549663200,
                "summary": "Rain overnight.",
                "icon": "rain",
                "sunriseTime": 1549684188,
                "sunsetTime": 1549731414,
                "moonPhase": 0.14,
                "precipIntensity": 0.221,
                "precipIntensityMax": 0.7645,
                "precipIntensityMaxTime": 1549670400,
                "precipProbability": 0.27,
                "precipType": "rain",
                "temperatureHigh": 27.92,
                "temperatureHighTime": 1549713600,
                "temperatureLow": 16.84,
                "temperatureLowTime": 1549760400,
                "apparentTemperatureHigh": 27.92,
                "apparentTemperatureHighTime": 1549713600,
                "apparentTemperatureLow": 16.93,
                "apparentTemperatureLowTime": 1549760400,
                "dewPoint": 14.08,
                "humidity": 0.67,
                "pressure": 1011.76,
                "windSpeed": 0.19,
                "windGust": 6.53,
                "windGustTime": 1549688400,
                "windBearing": 145,
                "cloudCover": 0.94,
                "uvIndex": 7,
                "uvIndexTime": 1549702800,
                "visibility": 16.09,
                "ozone": 253.47,
                "temperatureMin": 16.64,
                "temperatureMinTime": 1549677600,
                "temperatureMax": 27.92,
                "temperatureMaxTime": 1549713600,
                "apparentTemperatureMin": 16.64,
                "apparentTemperatureMinTime": 1549677600,
                "apparentTemperatureMax": 27.92,
                "apparentTemperatureMaxTime": 1549713600
            },
            {
                "time": 1549749600,
                "summary": "Rain in the morning.",
                "icon": "rain",
                "sunriseTime": 1549770632,
                "sunsetTime": 1549817775,
                "moonPhase": 0.17,
                "precipIntensity": 1.0668,
                "precipIntensityMax": 4.2316,
                "precipIntensityMaxTime": 1549760400,
                "precipProbability": 0.43,
                "precipType": "rain",
                "temperatureHigh": 24.07,
                "temperatureHighTime": 1549803600,
                "temperatureLow": 17.25,
                "temperatureLowTime": 1549854000,
                "apparentTemperatureHigh": 24.07,
                "apparentTemperatureHighTime": 1549803600,
                "apparentTemperatureLow": 17.25,
                "apparentTemperatureLowTime": 1549854000,
                "dewPoint": 14.73,
                "humidity": 0.72,
                "pressure": 1011.87,
                "windSpeed": 2.52,
                "windGust": 7.58,
                "windGustTime": 1549778400,
                "windBearing": 358,
                "cloudCover": 0.72,
                "uvIndex": 14,
                "uvIndexTime": 1549792800,
                "visibility": 15.58,
                "ozone": 257.27,
                "temperatureMin": 16.84,
                "temperatureMinTime": 1549760400,
                "temperatureMax": 24.07,
                "temperatureMaxTime": 1549803600,
                "apparentTemperatureMin": 16.93,
                "apparentTemperatureMinTime": 1549760400,
                "apparentTemperatureMax": 24.07,
                "apparentTemperatureMaxTime": 1549803600
            },
            {
                "time": 1549836000,
                "summary": "Mostly cloudy starting in the afternoon.",
                "icon": "partly-cloudy-night",
                "sunriseTime": 1549857075,
                "sunsetTime": 1549904134,
                "moonPhase": 0.2,
                "precipIntensity": 0.0381,
                "precipIntensityMax": 0.2489,
                "precipIntensityMaxTime": 1549886400,
                "precipProbability": 0.13,
                "precipType": "rain",
                "temperatureHigh": 26.19,
                "temperatureHighTime": 1549890000,
                "temperatureLow": 15.78,
                "temperatureLowTime": 1549944000,
                "apparentTemperatureHigh": 26.19,
                "apparentTemperatureHighTime": 1549890000,
                "apparentTemperatureLow": 15.78,
                "apparentTemperatureLowTime": 1549944000,
                "dewPoint": 12.99,
                "humidity": 0.61,
                "pressure": 1008.71,
                "windSpeed": 2.54,
                "windGust": 8.12,
                "windGustTime": 1549857600,
                "windBearing": 326,
                "cloudCover": 0.47,
                "uvIndex": 15,
                "uvIndexTime": 1549879200,
                "visibility": 16.09,
                "ozone": 246.75,
                "temperatureMin": 17.25,
                "temperatureMinTime": 1549854000,
                "temperatureMax": 26.19,
                "temperatureMaxTime": 1549890000,
                "apparentTemperatureMin": 17.25,
                "apparentTemperatureMinTime": 1549854000,
                "apparentTemperatureMax": 26.19,
                "apparentTemperatureMaxTime": 1549890000
            },
            {
                "time": 1549922400,
                "summary": "Rain in the afternoon.",
                "icon": "rain",
                "sunriseTime": 1549943518,
                "sunsetTime": 1549990493,
                "moonPhase": 0.24,
                "precipIntensity": 0.3912,
                "precipIntensityMax": 1.6332,
                "precipIntensityMaxTime": 1549972800,
                "precipProbability": 0.48,
                "precipType": "rain",
                "temperatureHigh": 22.24,
                "temperatureHighTime": 1549983600,
                "temperatureLow": 16.23,
                "temperatureLowTime": 1550030400,
                "apparentTemperatureHigh": 22.24,
                "apparentTemperatureHighTime": 1549983600,
                "apparentTemperatureLow": 16.23,
                "apparentTemperatureLowTime": 1550030400,
                "dewPoint": 13.89,
                "humidity": 0.72,
                "pressure": 1008.8,
                "windSpeed": 1.88,
                "windGust": 8.13,
                "windGustTime": 1549972800,
                "windBearing": 1,
                "cloudCover": 0.94,
                "uvIndex": 6,
                "uvIndexTime": 1549958400,
                "visibility": 16.09,
                "ozone": 244.47,
                "temperatureMin": 15.78,
                "temperatureMinTime": 1549944000,
                "temperatureMax": 22.24,
                "temperatureMaxTime": 1549983600,
                "apparentTemperatureMin": 15.78,
                "apparentTemperatureMinTime": 1549944000,
                "apparentTemperatureMax": 22.24,
                "apparentTemperatureMaxTime": 1549983600
            }
        ]
    },
    "offset": 2
}

我现在想要实现的是提取除今天或昨天之外的接下来 5 天的 temperatureHigh 和 temperatureLow。

基于上述代码从今天 2019-02-05 开始更新,前两天需要排除。所以我想开始我从时间戳1549490400提取的预测数据

我想最终得到的是以下回显数据的显示。

Johannesburg
Wednesday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Thursday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Friday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Saturday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Sunday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Monday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C

我也想只做一个这样的 3 天预测

Johannesburg
Wednesday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Thursday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Friday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C

在我说 [weathericon] 的地方,我需要例如从每天提取 "icon": "rain" 并将其与本地存储的同名 .jpg / .png 匹配。 IE。 rain.png 或 rain.jpg

我已经尝试了各种循环,这里有其他答案的 foreach 示例,但似乎没有什么能让我正确显示数据。

这是我部分工作的东西,但由于我对 JSON 缺乏了解,不知道为什么我会得到所有空白行。

<?php
header('Content-Type: application/json');
$json_string = file_get_contents("https://api.darksky.net/forecast/MYAPIKEY/-26.202271,28.043631?exclude=[currently,minutely,hourly,alerts,flags]&units=si");
$jsondata = json_decode($json_string, true);
//Echo Data
foreach ($jsondata as $max) {
  echo "MAX: ".$max['data'][2]['temperatureHigh']."\n";
  echo "MAX: ".$max['data'][3]['temperatureHigh']."\n";
  echo "MAX: ".$max['data'][4]['temperatureHigh']."\n";
}
?>

上面给了我这个输出

MAX: 
MAX: 
MAX: 
MAX: 
MAX: 
MAX: 
MAX: 
MAX: 
MAX: 
MAX: 25.43
MAX: 25.17
MAX: 27.92
MAX: 
MAX: 
MAX: 

【问题讨论】:

  • 如果你知道你需要的数组结构和键,你为什么要使用foreach
  • 我发布了上面的 foreach 示例,因为这是我在任何地方找到的唯一一个显示任何数据的 sn-p。我尝试了像 echo $jsonData['data'][2]['temperatureHigh']."&lt;br/&gt;"; 这样的简单回显,但它们只是给了我完全空白的输出,然后我也尝试像这样 echo 'MAX: ' . $jsonData-&gt;data[2]-&gt;temperatureHigh; 但也没有显示数据。

标签: arrays json weather-api


【解决方案1】:

您缺少正确的数组索引。去:

foreach($jsondata['daily']['data'] as $dayData) {
    $dayData['temperatureHigh'];
}

【讨论】:

  • 谢谢,现在可以很好地显示所有 8 天,我将如何排除前两天?同样,我怎样才能排除前 2 和后 3 只给我周三、周四和周五的 3 天展望?
  • 如果 API 在固定位置发送天数(第一天总是星期一,第三天是星期三),我会制作一系列指标来跳过。 foreach($jsondata['daily']['data'] as $dayIndex => $dayData) { if(!in_array($dayIndex,$arrayOfDaysToSkip,true)) { processDay(); } }
  • $arrayOfDaysToSkip = [0,1,5,6,7]; //或者类似的东西
  • 您可以随时将 $dayData['time'] 解析为 \DateTime 并从那里开始
  • 非常感谢您的帮助,我真的很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2011-08-05
  • 2019-04-27
  • 2019-04-19
  • 2021-12-23
相关资源
最近更新 更多