flibidi67/open-meteo

Symfony SDK for Open-Meteo API

Installs: 20

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Forks: 0

Type:symfony-bundle

1.0.0 2023-03-29 07:14 UTC

This package is not auto-updated.

Last update: 2024-04-24 12:29:54 UTC


README

Latest Version on Packagist Total Downloads

Open-Meteo

Open-Meteo collaborates with National Weather Services providing Open Data with 11 to 2 km resolution. Our high performance APIs select the best weather model for your location and provide data as a simple JSON API.

APIs are free without any API key for open-source developers and non-commercial use. You can embed them directly into your app.

Installation

You can install the package via composer:

composer require flibidi67/open-meteo

Then you can add this yaml file inside your config/packages directory (or you can copy it from the config directory of the package) :

flibidi67_open_meteo:
  default:
    temperature_unit: "celcius" # or fahrenheit
    wind_speed_unit: "kmh" # or ms, mph, kn
    precipitation_unit: "mm" # or inch
    timeformat: "iso8601" #or unixtime
    timezone: "GMT" # auto, GMT or another existing timezone (eg: Europe/Paris).
    past_days: 0 # between 0 and 92
    forecast_days: 7 # between 0 and 16
    current_weather: false # true or false

  forecast:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~
    past_days: ~
    forecast_days: ~
    current_weather: ~

  historical:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~

  ecmwf:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    past_days: ~

  gfs:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~
    past_days: ~
    forecast_days: ~
    current_weather: ~

  meteofrance:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~
    past_days: ~
    current_weather: ~

  dwd:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~
    past_days: ~
    current_weather: ~

  jma:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~
    past_days: ~
    current_weather: ~

  metno:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~
    past_days: ~
    current_weather: ~

  gem:
    temperature_unit: ~
    wind_speed_unit: ~
    precipitation_unit: ~
    timeformat: ~
    timezone: ~
    past_days: ~
    current_weather: ~

You can either modify the values for a specific API's endpoint or just the default values. If a value is set to null for API's parameters, then the default value is taken.

Usage

/!\ To see all available parameters don't hesitate to take a look at the Open-Meteo Documentation and feel free to explore the code.

List of available services

Main service's methods

You can easily chain the different values you want to retrieve (except after retrieving the Settings). Autosuggest from your IDE will be you best friend :)

Daily, Hourly and Minutely15

  • with(string|array $parameter) : $parameter can contain coma.
  • get(): call the service's get() method.
    $service->setCoordinates(48.58, 7.75)
         ->getHourly()
         ->with("temperature_2m,relativehumidity_2m,dewpoint_2m,cloudcover,cloudcover_low,cloudcover_mid");
    

Use a formatter

In order to directly render the data returned by the OpenMeteo API in a desired format, you can set a formatter to use. This package come with a DefaultFormatter. To use it :

$myService->useDefaultFormatter()

The default formatter return the hourly, daily and minutely_15 in a formatted associative array.

[
    'hourly' => [
        '2023-03-22 00:00' => [
            'time' => DateTime // the datetime object corresponding to the key
            'temperature_2m' => 25.9 // for example
            // and all the other values you retrieve with the with function
        ]   
    ]
]

If you want to use your own formatter you should do like this

namespace Your\Namespace;

use Flibidi67\OpenMeteo\Contract\FormatterInterface;

class MyAwesomeFormatter implements FormatterInterface {
    public function format(array $data): array {
        // do what you want with $data.
        return [];    
    }
}
// And then to use it
$myService->useFormatter(new MyAwesomeFormatter());

Quick examples

public function myFunction(ForecastService $forecastService) {
    $forecastService->setCoordinates(48.58, 7.75)
        ->getHourly()
        ->withApparentTemperature()
        ->withTemperature2m()
        ->getSettings()
        ->setCurrentWeather(true)
        ->getDaily()
        ->withApparentTemperatureMax();
        
    $results = $forecastService->get();
}

public function myFunction(ForecastService $forecastService) {
    $forecastService->setCoordinates(48.58, 7.75)
        ->getHourly()
        ->withAll(true) // true parameters is for precise that we want ALL the pressure level.
        ->getDaily()
        ->withAll();
        
    $results = $forecastService->get();
}

public function myFunction(ForecastService $forecastService) {
    $results = $forecastService->setCoordinates(48.58, 7.75)
        ->getHourly()
        ->withAll(true) // true parameters is for precise that we want ALL the pressure level.
        ->getDaily()
        ->withAll()
        ->get();
}

public function myFunction(HttpClientInterface $httpClient, ParameterBagInterface $parameterBag) {
    $forecastService = new ForecastService($httpClient, $parameterBag);
    $forecastService->setCoordinates(48.58, 7.75)->withHourlyTemperature2m();
}