solgenpower / laravel-openweather
A Laravel SDK for OpenWeather API
Installs: 1 625
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 2
Open Issues: 4
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.5
- illuminate/http: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
Requires (Dev)
- laravel/pint: ^1.6
- nunomaduro/larastan: ^2.5
- orchestra/testbench: ^7.0|^8.0
- phpunit/phpunit: ^9.6
README
A Laravel SDK for OpenWeather API
Installation
Install the package via composer:
composer require solgenpower/laravel-openweather
If you're interested in modifying the config file, then publish it using the following command:
php artisan vendor:publish --provider="SolgenPower\LaravelOpenWeather\OpenWeatherServiceProvider"
This is the contents of the published config file:
return [ /** * API Key for Open Weather */ 'api-key' => env('OPENWEATHER_API_KEY', ''), /** * Endpoint for the Current Weather */ 'current-endpoint' => env('OPENWEATHER_CURRENT_ENDPOINT', 'https://api.openweathermap.org/data/2.5/weather/'), /** * Endpoint for the Weather Condition icons * Reference: https://openweathermap.org/weather-conditions */ 'icon-endpoint' => env('OPENWEATHER_ICON_ENDPOINT', 'https://openweathermap.org/img/wn/'), /** * Map icon code to actual icon filenames */ 'icon-map' => [ /** * Day Icons */ '01d' => '01d.png', '02d' => '02d.png', '03d' => '03d.png', '04d' => '04d.png', '09d' => '09d.png', '10d' => '10d.png', '11d' => '11d.png', '13d' => '13d.png', '50d' => '50d.png', /** * Night Icons */ '01n' => '01n.png', '02n' => '02n.png', '03n' => '03n.png', '04n' => '04n.png', '09n' => '09n.png', '10n' => '10n.png', '11n' => '11n.png', '13n' => '13n.png', '50n' => '50n.png', ], /** * Cache duration default in seconds, 60 * 10 is 10 minutes */ 'cache-duration' => 60 * 10, /** * standard => Kelvin * imperial => Fahrenheit * metric => Celsius */ 'temperature-unit' => env('OPENWEATHER_TEMPERATURE_UNIT', 'imperial'), ];
Usage
You can get weather information by providing coordinates
$whiteHouseWeather = OpenWeather::coordinates("38.897957", "-77.036560"); echo $whiteHouseWeather->humidity; //64
or by zip code
$californiaWeather = OpenWeather::zip('90210', 'US'); echo $californiaWeather->windDirection; //N
or by city name
$pheonixWeather = OpenWeather::city('Tucson', 'AZ', 'US'); echo $pheonixWeather->feelsLike; //281.55
All these methods will return a Weather DTO that looks like this:
class Weather { public function __construct( public readonly float $latitude, public readonly float $longitude, public readonly ?string $countryCode, public readonly ?string $city, public readonly string $condition, public readonly string $description, public readonly string $icon, public readonly float $temperature, public readonly ?float $feelsLike, public readonly ?int $pressure, public readonly ?int $humidity, public readonly ?float $windSpeed, public readonly ?int $windAngle, public readonly ?string $windDirection, public readonly ?int $cloudiness, public readonly ?int $visibility, /** * Seconds difference from UTC * To use with Carbon's timezone method, divide by 3600 */ public readonly int $timezone, public readonly Carbon $sunrise, public readonly Carbon $sunset, public readonly Carbon $calculatedAt ) { } }
Testing
composer review