Skip to main content

Weather Forecast API

This API allows you to retrieve 16-day/daily and 5-day/3-hourly weather forecasts.

Corresponding OpenWeatherMap Documentation:

The $owm->getWeatherForecast() method takes the following parameters:

NameTypeDefaultDescription
$querymixed--See below
$units"imperial"|"metric""imperial"Units to use
$langstringenOne of the languages listed at the very bottom of the official documentation
$appidstring''Deprecated, always set to ''
$daysint (1 - 16)1Number of days to retrieve the forecast for. If $days is between 1 and 5, the 5-day/3-hourly forecast API is used. If $days is between 6 and 16, the 16-day/daily forecast API is used.

You can use ->getDailyWeatherForecast() if instead if you want to retrieve a daily forecast even when a 3-horuly forecast is available.

$query parameter

The first parameter determines the location to get weather data from. Several possible approaches are possible:

by city name

Specifying the country is optional.

$forecasts = $owm->getWeatherForecast('Berlin,DE', $units, $lang, '', $days);

by city id

One city id:

$forecasts = $owm->getWeatherForecast(2172797, $units, $lang, '', $days);

by zip code

Specifying the country is optional.

// Hyderabad, India
$forecasts = $owm->getWeatherForecast('zip:500001,IN', $units, $lang, '', $days);

by coordinates

$forecasts = $owm->getWeatherForecast(['lat' => 77.73038, 'lon' => 41.89604],
$units, $lang, '', $days);

$forecasts object

The $forecasts is an instance of Cmfcmf\OpenWeatherMap\WeatherForecast. It provides the following data:

NameTypeDescription
lastUpdate\ḐateTimeInterfaceWhen the data was last updated
city->idintInternal city id
city->namestringName of the city
city->countrystringCity country code
city->timezone\DateTimeZone|nullCity timezone
city->lonfloatCity longitude
city->latfloatCity latitude

To retrieve the forecasts, iterate over the object:

foreach ($forecasts as $forecast) {
// Do something
}

$forecast object

The $forecast is an instance of Cmfcmf\OpenWeatherMap\Forecast that extends the Cmfcmf\OpenWeatherMap\CurrentWeather object.

NameTypeDescription
time->day\DateTimeInterfaceThe day of the forecast
time->from\DateTimeInterfaceThe exact start time of the forecast. For 16-day/daily forecasts, this corresponds to time->day. For 5-day/3-hourly forecasts, it corresponds to the start of the 3-hour window.
time->to\DateTimeInterfaceThe exact end time of the forecast. For 16-day/daily forecasts, this corresponds to time->day at 23:59:59. For 5-day/3-hourly forecasts, it corresponds to the end of the 3-hour window.
......All other properties from the CurrentWeather object, as described here.

only for 5-day/3-hourly forecasts

NameTypeDescription
temperature->nowUnitNote: This should be named temperature->avg and is only named temperature->now for backwards compatibility! Returns the average temperature for the given location (i.e, a big city might have multiple temperature measurement stations)
temperature->minUnitMinimum temperature for the given locaiton
temperature->maxUnitMaximum temperature for the given locaiton

only for 16-day/hourly forecasts

NameTypeDescription
temperature->morningUnitTemperature at morning
temperature->nowUnitTemperature at day
temperature->eveningUnitTemperature at evening
temperature->nightUnitTemperature at night
temperature->minUnitMinimum temperature at day
temperature->maxUnitMaximum temperature at day