rubin / openweather
There is no license information available for the latest version (1.2.1) of this package.
OpenWeather API connector
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 implementation for the OpenWeather REST API. This library is based on the REST API docs.
Installation
With composer:
composer require rubin/openweather
Usage
Create API connector:
$openWeatherApi = new \OpenWeather\OpenWeatherApi('{key}');
Set language (optional):
$openWeatherApi->setLanguage('ru');
Examples
Example script to get current weather:
$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();
Example script to get 5 days forecast:
$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();