rubin / openweather
此包的最新版本(1.2.1)没有可用的许可证信息。
OpenWeather API 连接器
1.2.1
2024-07-22 13:00 UTC
Requires
- php: >=8.1.0
- ext-json: *
- cuyz/valinor: ^1.12
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.7
- phpunit/phpunit: ^10.5
README
PHP 实现用于 OpenWeather REST API。此库基于 REST API 文档。
安装
使用 composer
composer require rubin/openweather
用法
创建 API 连接器
$openWeatherApi = new \OpenWeather\OpenWeatherApi('{key}');
设置语言(可选)
$openWeatherApi->setLanguage('ru');
示例
获取当前天气的示例脚本
$openWeatherApi = new \OpenWeather\OpenWeatherApi('{key}'); $output = new \Symfony\Component\Console\Output\StreamOutput(fopen('php://stdout', 'w')); $table = new \Symfony\Component\Console\Helper\Table($output); $table ->setHeaders(['Latitude', 'Longitude', 'Temperature', 'Weather']) ->setRows(array_map(function (\OpenWeather\GeoCoordinates $coordinates) use ($openWeatherApi) { $current = $openWeatherApi->getCurrentWeather($coordinates); return [ $coordinates->lat, $coordinates->lon, $current->main->temp, $current->weather[0]->description ]; }, [ new \OpenWeather\GeoCoordinates(lon: 37.36, lat: 55.45), new \OpenWeather\GeoCoordinates(lon: -66.159, lat: -68.2008), new \OpenWeather\GeoCoordinates(lon: 147.794, lat: -31.358) ])); $table->render();
获取 5 天预报的示例脚本
$openWeatherApi = new \OpenWeather\OpenWeatherApi('{key}'); $output = new \Symfony\Component\Console\Output\StreamOutput(fopen('php://stdout', 'w')); $table = new \Symfony\Component\Console\Helper\Table($output); $table ->setHeaders(['DateTime', 'Temperature', 'PoP', 'Weather']) ->setRows(array_map(fn(\OpenWeather\ForecastItem $item) => [ $item->dt->format('Y-m-d H:i:s'), $item->main->temp, $item->pop, $item->weather[0]->description, ], $openWeatherApi->getForecast(new \OpenWeather\GeoCoordinates(lon: -66.159, lat: -68.2008))->list)); $table->render();