flibidi67/open-meteo

Open-Meteo API 的 Symfony SDK

安装: 27

依赖项: 0

建议者: 0

安全: 0

星标: 1

分支: 0

类型:symfony-bundle

1.0.0 2023-03-29 07:14 UTC

This package is not auto-updated.

Last update: 2024-09-25 14:22:23 UTC


README

Latest Version on Packagist Total Downloads

Open-Meteo

Open-Meteo 与国家气象服务机构合作,提供具有 11 到 2 公里分辨率的开放数据。我们高性能的 API 为您的位置选择最佳的天气模型,并作为简单的 JSON API 提供数据。

API 对开源开发者和非商业用途免费,无需任何 API 密钥。您可以直接将其嵌入到您的应用程序中。

安装

您可以通过 composer 安装此包

composer require flibidi67/open-meteo

然后您可以在您的 config/packages 目录内添加此 yaml 文件(或您可以从包的 config 目录中复制它)

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: ~

您可以选择修改特定 API 的端点值或只是默认值。如果 API 参数设置为 null,则采用默认值。

使用方法

/!\ 要查看所有可用参数,请查看 Open-Meteo 文档,并自由探索代码.

可用服务列表

主要服务方法

您可以将要检索的不同值轻松链接起来(除了在检索设置之后)。您的 IDE 的自动建议将是您最好的朋友 :)

每日、每小时和每15分钟

  • with(string|array $parameter) : $parameter 可以包含逗号。
  • get(): 调用服务的 get() 方法。
    $service->setCoordinates(48.58, 7.75)
         ->getHourly()
         ->with("temperature_2m,relativehumidity_2m,dewpoint_2m,cloudcover,cloudcover_low,cloudcover_mid");
    

使用格式化程序

为了直接以所需的格式渲染 OpenMeteo API 返回的数据,您可以设置一个要使用的格式化程序。此包附带一个 DefaultFormatter。要使用它

$myService->useDefaultFormatter()

默认格式化程序将 hourlydailyminutely_15 返回为格式化的关联数组。

[
    '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
        ]   
    ]
]

如果您想使用自己的格式化程序,应这样做

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());

快速示例

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();
}