solgenpower / laravel-openweather
OpenWeather API 的 Laravel SDK
v0.3.0
2023-06-07 09:30 UTC
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
OpenWeather API 的 Laravel SDK
安装
使用 composer 安装包
composer require solgenpower/laravel-openweather
如果您想修改配置文件,请使用以下命令发布它
php artisan vendor:publish --provider="SolgenPower\LaravelOpenWeather\OpenWeatherServiceProvider"
这是已发布配置文件的内容
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'), ];
使用方法
您可以通过提供坐标来获取天气信息
$whiteHouseWeather = OpenWeather::coordinates("38.897957", "-77.036560"); echo $whiteHouseWeather->humidity; //64
或者通过邮政编码
$californiaWeather = OpenWeather::zip('90210', 'US'); echo $californiaWeather->windDirection; //N
或者通过城市名称
$pheonixWeather = OpenWeather::city('Tucson', 'AZ', 'US'); echo $pheonixWeather->feelsLike; //281.55
所有这些方法都将返回一个类似这样的 Weather DTO
class Weather { public function __construct( public readonly float $latitude, public readonly float $longitude, public readonly ?string $countryCode, 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 ) { } }
测试
composer review