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
// 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
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
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.