voku/weather

天气API包装器


README

SWUbanner

Build Status codecov.io

天气数据包装器

这是一个围绕“Bright Sky”(https://brightsky.dev/)天气API的简单包装器。

  • 您可以通过实现另一个天气提供者类简单地替换天气API。

演示:(https://github.com/voku/weather-demo)

http://weather-demo.suckup.de/

通过"composer require"安装

composer require voku/weather
composer require voku/httpful # or another php-http/client-implementation e.g. "php-http/react-adapter" + "nyholm/psr7"

用法

use voku\weather\provider\BrightskyHttpProvider;
use voku\weather\WeatherQueryDto;

require_once __DIR__ . '/vendor/autoload.php'; // example path

$latitude = 48.137154;
$longitude = 11.576124;
$dateTime = new \DateTimeImmutable('2023-01-01 12:00:00');

$weatherQuery = new WeatherQueryDto(
    $latitude,
    $longitude,
    $dateTime
);

$weather = (new BrightskyHttpProvider())->getWeatherHistorical($weatherQuery);

echo $weather->temperature; // 17.1

示例 1: (带有单位的温度)

echo $weather->getTemperatureWithUnit(); // 17.1 °C

示例 2: (带有单位的风速)

echo $weather->getWindSpeedWithUnit(); // 9 km/h

单元测试

  1. Composer是运行测试的前提条件。
composer install
  1. 可以从根目录运行此命令来执行测试
./vendor/bin/phpunit

AbstractHttpProvider 方法

getSources(): WeatherSourceDto[]

参数:

返回

  • \WeatherSourceDto[]

getWeatherCurrent(voku\weather\WeatherQueryDto $query)

参数

  • voku\weather\WeatherQueryDto $query

返回

  • voku\weather\WeatherDto

getWeatherForecast(voku\weather\WeatherQueryDto $query)

参数

  • voku\weather\WeatherQueryDto $query

返回

  • voku\weather\WeatherDto

getWeatherForecastCollection(voku\weather\WeatherQueryDto $query)

参数

  • voku\weather\WeatherQueryDto $query

返回

  • voku\weather\WeatherCollection

getWeatherHistorical(voku\weather\WeatherQueryDto $query)

参数

  • voku\weather\WeatherQueryDto $query

返回

  • voku\weather\WeatherDto

getWeatherHistoricalCollection(voku\weather\WeatherQueryDto $query)

参数

  • voku\weather\WeatherQueryDto $query

返回

  • voku\weather\WeatherCollection

感谢

  • 感谢GitHub (Microsoft) 提供代码托管和良好的基础设施,包括问题管理等。
  • 感谢IntelliJ,因为他们提供了最好的PHP IDE,并为我提供了PhpStorm的开源许可证!
  • 感谢StyleCI提供简单但强大的代码风格检查。
  • 感谢PHPStan && Psalm 提供真正优秀的静态分析工具,并发现代码中的错误!