PHP code example of dle79 / open-meteo

1. Go to this page and download the library: Download dle79/open-meteo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dle79 / open-meteo example snippets


use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Orionphp\OpenMeteo\OpenMeteoClient;
use Orionphp\OpenMeteo\Request\ForecastRequestBuilder;
use Orionphp\OpenMeteo\Enum\WeatherModel;
use Orionphp\OpenMeteo\Enum\CurrentField;
use Orionphp\OpenMeteo\Enum\HourlyField;

$httpFactory = new HttpFactory();
$client = new OpenMeteoClient(
    httpClient: new Client(),
    requestFactory: $httpFactory,
);

$request = ForecastRequestBuilder::create(52.52, 13.41)         // Berlin coordinates
    ->models(WeatherModel::ICON_D2)     // WeatherModel::ICON_D2);
$times  = $hourly->time(); // list of ISO 8601 strings

$request = ForecastRequestBuilder::create(float $latitude, float $longitude)
    ->models(WeatherModel ...$models)       //    ->minutely15(Minutely15Field ...$fields)
    ->hourly(HourlyField ...$fields)
    ->daily(DailyField ...$fields)
    ->build();

use Orionphp\OpenMeteo\Enum\WeatherModel;

// Automatically select the most precise models for a coordinate
$models = WeatherModel::recommendedFor(52.52, 13.41);

// Get only the single best model
$model = WeatherModel::bestFor(52.52, 13.41);

// Introspect a model
WeatherModel::ICON_D2->isGlobal();    // false
WeatherModel::ICON_D2->isEuropean();  // true
WeatherModel::ICON_D2->isRegional();  // true

$request = ForecastRequestBuilder::create(48.85, 2.35)   // Paris
    ->models(WeatherModel::METEOFRANCE_AROME, WeatherModel::ECMWF_IFS)
    ->hourly(HourlyField::TEMPERATURE_2M)
    ->build();

$forecast = $client->forecast($request);
$field    = $forecast->hourly->field(HourlyField::TEMPERATURE_2M);

$aromeTemps = $field->values(WeatherModel::METEOFRANCE_AROME);
$ecmwfTemps = $field->values(WeatherModel::ECMWF_IFS);

use Orionphp\OpenMeteo\Enum\CurrentField;

// Available fields
CurrentField::TEMPERATURE_2M
CurrentField::APPARENT_TEMPERATURE
CurrentField::WEATHER_CODE
CurrentField::RELATIVE_HUMIDITY_2M
CurrentField::WIND_SPEED_10M
CurrentField::WIND_DIRECTION_10M
CurrentField::WIND_GUSTS_10M
CurrentField::PRECIPITATION
CurrentField::RAIN
CurrentField::SNOWFALL
CurrentField::SNOW_DEPTH
CurrentField::CLOUD_COVER
CurrentField::PRESSURE_MSL
CurrentField::SURFACE_PRESSURE
CurrentField::VISIBILITY
CurrentField::UV_INDEX
CurrentField::DEW_POINT_2M
CurrentField::IS_DAY
CurrentField::PRECIPITATION_PROBABILITY
CurrentField::SHOWERS

// Usage
$current = $forecast->current;          // ?CurrentData
$current->time();                        // e.g. "2026-03-17T12:00"
$data = $current->field(CurrentField::TEMPERATURE_2M);
$data->value();                          // float|int|string|null
$data->unit();                           // e.g. "°C"

use Orionphp\OpenMeteo\Enum\Minutely15Field;

$minutely = $forecast->minutely15;       // ?Minutely15Data
$minutely->time();                       // list<string>
$minutely->field(Minutely15Field::PRECIPITATION)->values(WeatherModel::ICON_D2);

use Orionphp\OpenMeteo\Enum\HourlyField;

$hourly = $forecast->hourly;             // ?HourlyData
$hourly->time();                         // list<string>
$hourly->field(HourlyField::UV_INDEX)->values(WeatherModel::ECMWF_IFS);
$hourly->availableFields();              // list<HourlyField>

use Orionphp\OpenMeteo\Enum\DailyField;

$daily = $forecast->daily;               // ?DailyData
$daily->time();                          // list<string> — one entry per day
$daily->field(DailyField::TEMPERATURE_2M_MAX)->values(WeatherModel::ICON_D2);

use Orionphp\OpenMeteo\Enum\WeatherCode;
use Orionphp\OpenMeteo\Enum\Locale;
use Orionphp\OpenMeteo\Translation\WeatherCodeTranslation;

$translator = new WeatherCodeTranslation();

$code = WeatherCode::fromInt(63); // ModerateRain
$translator->translate($code, Locale::EN); // "Moderate rain"
$translator->translate($code, Locale::DE); // "Mäßiger Regen"
$translator->translate($code, Locale::FR); // "Pluie modérée"
$translator->translate($code, Locale::ES); // "Lluvia moderada"

$translator = new WeatherCodeTranslation('/path/to/your/locales');

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('open-meteo');
$logger->pushHandler(new StreamHandler('php://stdout'));

$client = new OpenMeteoClient(
    httpClient: new Client(),
    requestFactory: $httpFactory,
    logger: $logger,
);

use Orionphp\OpenMeteo\Exception\OpenMeteoException;
use Orionphp\OpenMeteo\Exception\InvalidCoordinatesException;
use Orionphp\OpenMeteo\Exception\InvalidTimezoneException;
use Orionphp\OpenMeteo\Exception\InvalidWeatherModelException;

try {
    $forecast = $client->forecast($request);
} catch (InvalidCoordinatesException $e) {
    // Latitude/longitude out of valid range
} catch (InvalidTimezoneException $e) {
    // Timezone string not recognized by PHP
} catch (InvalidWeatherModelException $e) {
    // WeatherModel::fromString() called with unknown value
} catch (OpenMeteoException $e) {
    // HTTP error, non-2xx status, or malformed JSON response
}