仅在5天而不是每小时开放的weatherAPI中循环播放

时间:2019-03-07 00:48:22

标签: php openweathermap

我正在尝试显示来自开放式天气api的数据,并且我已经成功使用了该api并从中输出了一些数据。但是,我正在使用的API给我每小时的数据。但是我只想要API响应中所有5天的第一个数据集。

我遍历了我想要的所有字段,但是过去3个小时给了我每个字段。我只想要5天的字段。 (它应该给我5个结果,但在过去5天里每小时都给我结果)

所以我的问题是,如何通过遍历API响应仅获得5天的结果,而不是每小时获取一次呢? 这是我的代码:

$currentjson_string = file_get_contents("https://api.openweathermap.org/data/2.5/weather?q=Shanghai&appid=&units=imperial");
$forecastjson_string = file_get_contents("https://api.openweathermap.org/data/2.5/forecast?q=Shanghai&appid=&units=imperial");

$currentParsed = json_decode($currentjson_string, true);
$forecastParsed = json_decode($forecastjson_string, true);


// City Variables

$country = $currentParsed['name'];
$city = $currentParsed['sys']['country'];

// Current Weather Variables

$currentWeatherLongitude = $currentParsed['coord']['lon'];
$currentWeatherLatitude = $currentParsed['coord']['lat'];
$currentIcon = $currentParsed['weather'][0]['icon'];
$currentWeatherF = $currentParsed['main']['temp'];
$currentWeatherconditions = $currentParsed['weather'][0]['description'];
// F to C
$currentWeatherC = ($currentWeatherF - 32) * 5 / 9;
$currentWeathermin = $currentParsed['main']['temp_min'];
$currentWeathermax = $currentParsed['main']['temp_max'];

<div class="weather-info">
<div class="weather-centered-box">
<span class="weather-description" style="font-size:40px;"><center><?php echo $country . ', ' . $city ?></center></span>
<span class="current-conditions">Current Conditions</span> 
<span class="weather-conditions"><center><?php echo ucwords($currentWeatherconditions) ?></center></span>
<span class="weather-location" style="font-size:24px;"><?php echo 'Latitude ' . $currentWeatherLatitude . ', Longitude ' . $currentWeatherLongitude ?></span>
<span class="weather-icon"><center><?php echo '<img src="http://openweathermap.org/img/w/'.$currentIcon.'.png" />'; ?></center></span>

<?php

echo '<span class="weather-temp-f">'.$currentWeatherF.' <sup>&deg;</sup>F ('.round($currentWeatherC, 2).' <sup>&deg;</sup>C)</span>';

foreach ($forecastParsed['list'] as $key) {
  

// This is where im stuck. I don't want all the temperatures only 5 days worth of results from the temperature not hourly?
 echo $key['main']['temp'];

?>

API的JSON响应:

{
    "cod": "200",
    "message": 0.0042,
    "cnt": 39,
    "list": [
        {
            "dt": 1551927600,
            "main": {
                "temp": 53.04,
                "temp_min": 51.65,
                "temp_max": 53.04,
                "pressure": 1027.25,
                "sea_level": 1027.25,
                "grnd_level": 1026.28,
                "humidity": 64,
                "temp_kf": 0.77
            },
            "weather": [
                {
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "02d"
                }
            ],
            "clouds": {
                "all": 8
            },
            "wind": {
                "speed": 15.59,
                "deg": 18.5008
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2019-03-07 03:00:00"
        },
        {
            "dt": 1551938400,
            "main": {
                "temp": 54.28,
                "temp_min": 53.24,
                "temp_max": 54.28,
                "pressure": 1026.29,
                "sea_level": 1026.29,
                "grnd_level": 1025.32,
                "humidity": 56,
                "temp_kf": 0.58
            },
            "weather": [
                {
                    "id": 801,
                    "main": "Clouds",
                    "description": "few clouds",
                    "icon": "02d"
                }
            ],
            "clouds": {
                "all": 20
            },
            "wind": {
                "speed": 13.56,
                "deg": 20.0005
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2019-03-07 06:00:00"
        },
        {
            "dt": 1551949200,
            "main": {
                "temp": 49.8,
                "temp_min": 49.11,
                "temp_max": 49.8,
                "pressure": 1026.22,
                "sea_level": 1026.22,
                "grnd_level": 1025.34,
                "humidity": 54,
                "temp_kf": 0.39
            },
            "weather": [
                {
                    "id": 803,
                    "main": "Clouds",
                    "description": "broken clouds",
                    "icon": "04d"
                }
            ],
            "clouds": {
                "all": 64
            },
            "wind": {
                "speed": 11.74,
                "deg": 28.0067
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2019-03-07 09:00:00"
        },
        {
            "dt": 1551960000,
            "main": {
                "temp": 41.05,
                "temp_min": 40.71,
                "temp_max": 41.05,
                "pressure": 1027,
                "sea_level": 1027,
                "grnd_level": 1026.12,
                "humidity": 70,
                "temp_kf": 0.19
            },
            "weather": [
                {
                    "id": 801,
                    "main": "Clouds",
                    "description": "few clouds",
                    "icon": "02n"
                }
            ],
            "clouds": {
                "all": 24
            },
            "wind": {
                "speed": 9.89,
                "deg": 38.5044
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2019-03-07 12:00:00"
        },
        {
            "dt": 1551970800,
            "main": {
                "temp": 35.75,
                "temp_min": 35.75,
                "temp_max": 35.75,
                "pressure": 1027.53,
                "sea_level": 1027.53,
                "grnd_level": 1026.67,
                "humidity": 90,
                "temp_kf": 0
            },
            "weather": [
                {
                    "id": 802,
                    "main": "Clouds",
                    "description": "scattered clouds",
                    "icon": "03n"
                }
            ],
            "clouds": {
                "all": 48
            },
            "wind": {
                "speed": 6.4,
                "deg": 26.001
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2019-03-07 15:00:00"
        },
        {
            "dt": 1551981600,
            "main": {
                "temp": 34.54,
                "temp_min": 34.54,
                "temp_max": 34.54,
                "pressure": 1026.13,
                "sea_level": 1026.13,
                "grnd_level": 1025.29,
                "humidity": 94,
                "temp_kf": 0
            },
            "weather": [
                {
                    "id": 803,
                    "main": "Clouds",
                    "description": "broken clouds",
                    "icon": "04n"
                }
            ],
            "clouds": {
                "all": 64
            },
            "wind": {
                "speed": 3.62,
                "deg": 25.0002
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2019-03-07 18:00:00"
        },
        {
            "dt": 1551992400,
            "main": {
                "temp": 34.49,
                "temp_min": 34.49,
                "temp_max": 34.49,
                "pressure": 1025.72,
                "sea_level": 1025.72,
                "grnd_level": 1024.85,
                "humidity": 92,
                "temp_kf": 0
            },
            "weather": [
                {
                    "id": 804,
                    "main": "Clouds",
                    "description": "overcast clouds",
                    "icon": "04n"
                }
            ],
            "clouds": {
                "all": 88
            },
            "wind": {
                "speed": 3.38,
                "deg": 31.0012
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2019-03-07 21:00:00"
        },
        {
            "dt": 1552003200,
            "main": {
                "temp": 42.85,
                "temp_min": 42.85,
                "temp_max": 42.85,
                "pressure": 1026.72,
                "sea_level": 1026.72,
                "grnd_level": 1025.83,
                "humidity": 86,
                "temp_kf": 0
            },
            "weather": [
                {
                    "id": 804,
                    "main": "Clouds",
                    "description": "overcast clouds",
                    "icon": "04d"
                }
            ],
            "clouds": {
                "all": 88
            },
            "wind": {
                "speed": 7.49,
                "deg": 72.5035
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2019-03-08 00:00:00"
        }

1 个答案:

答案 0 :(得分:1)

您可以尝试类似的方法。由于未经测试,可能有必要修改下面的代码,但这可以使您了解如何解决此问题:

$previous_date = '';
foreach ($forecastParsed['list'] as $key) {
   $dt_txt = $key['dt_txt'];
   $date = new DateTime($dt_txt);
   $current_date = $date->format('Y-m-d');
   if ($current_date != $previous_date) { echo $key['main']['temp']; }
   $previous_date = $current_date;
}

它也可以写得更简洁(即在foreach中合并前两行以消除$dt_txt变量)。

逻辑解释:
foreach循环获取当前$key的日期时间,剥离时间戳并将其放入$current_date中。然后将$current_date$previous_date进行比较。如果它们不匹配,则我们知道这是新的一天,并且回显了温度。 $previous_date重置为$current_date,因此该变量中的值已准备好进行下一个循环。