dnsimmons / openweather
OpenWeather是一个简化使用免费Open Weather Map API的Laravel包。
This package is auto-updated.
Last update: 2024-09-25 21:07:08 UTC
README
关于
OpenWeather是一个Laravel包,简化了使用免费Open Weather Map API的操作。
OpenWeather负责向各种免费Open Weather Map API发送请求,并返回结构良好、易于使用的天气数据,包括条件、温度、湿度、压力、风速、位置和时间戳数据。
请参阅下面的示例使用部分,以获取典型的结构化响应。
支持的API
该包支持以下免费Open Weather Map API
免费API限制
- 每分钟60次调用,每月最多1,000,000次调用
- 使用一次性请求时,每天1000次调用
要求
- 有效的Open Weather Maps API密钥(AppID)。
安装
使用composer安装此包
$ composer require dnsimmons/openweather
对于Laravel >= 5.5,包会自动发现。
对于5.5之前的旧Laravel版本,将服务提供者添加到您的config/app.php
中,并添加一个别名
'providers' => [
...
Dnsimmons\OpenWeather\OpenWeatherServiceProvider::class,
];
'aliases' => [
...
'OpenWeather' => Dnsimmons\OpenWeather\OpenWeather::class,
];
使用Artisan命令发布所需的包配置文件
$ php artisan vendor:publish
编辑Laravel实例中的.env
文件,添加以下环境变量,并添加您的OpenWeather API密钥
OPENWEATHER_API_KEY="your-api-key"
OPENWEATHER_API_LANG="en"
OPENWEATHER_API_DATE_FORMAT="m/d/Y"
OPENWEATHER_API_TIME_FORMAT="h:i A"
OPENWEATHER_API_DAY_FORMAT="l"
示例使用
在下面的示例中,我们通过邮政编码获取当前天气。
$weather = new OpenWeather();
$current = $weather->getCurrentWeatherByPostal('02111');
print_r($current);
输出
Array
(
[formats] => Array
(
[lang] => en
[date] => m/d/Y
[day] => l
[time] => h:i A
[units] => imperial
)
[datetime] => Array
(
[timestamp] => 1593387767
[timestamp_sunrise] => 1593335394
[timestamp_sunset] => 1593390304
[formatted_date] => 06/28/2020
[formatted_day] => Sunday
[formatted_time] => 11:42 PM
[formatted_sunrise] => 09:09 AM
[formatted_sunset] => 12:25 AM
)
[location] => Array
(
[id] => 4930956
[name] => Boston
[country] => US
[latitude] => 42.36
[longitude] => -71.06
)
[condition] => Array
(
[id] => 801
[name] => Rain
[desc] => light rain
[icon] => https://openweathermap.org/img/w/10d.png
)
[wind] => Array
(
[speed] => 11.5
[deg] => 113
[direction] => SE
)
[forecast] => Array
(
[temp] => 67
[temp_min] => 64
[temp_max] => 68
[pressure] => 1007
[humidity] => 88
)
)
方法
所有方法在成功时返回数组,在失败时返回FALSE。
当前天气方法
getCurrentWeatherByCityName(string $city, string $units)
参数
- 城市名称(例如:波士顿)
- 单位(英制(默认),公制或开尔文)
getCurrentWeatherByCityId(int $id, string $units)
参数
- 城市ID
- 单位(英制(默认),公制或开尔文)
getCurrentWeatherByCoords(string $latitude, string $longitude, string $units)
参数
- 纬度
- 经度
- 单位(英制(默认),公制或开尔文)
getCurrentWeatherByPostal(string $postal, string $units)
参数
- 美国邮政编码
- 单位(英制(默认),公制或开尔文)
4天3小时预报方法
getForecastWeatherByCityName(string $city, string $units)
参数
- 城市名称(例如:波士顿)
- 单位(英制(默认),公制或开尔文)
getForecastWeatherByCityId(int $id, string $units)
参数
- 城市ID
- 单位(英制(默认),公制或开尔文)
getForecastWeatherByCoords(string $latitude, string $longitude, string $units)
参数
- 纬度
- 经度
- 单位(英制(默认),公制或开尔文)
getForecastWeatherByPostal(string $postal, string $units)
参数
- 美国邮政编码
- 单位(英制(默认),公制或开尔文)
一次性请求方法
getOnecallWeatherByCoords(string $latitude, string $longitude, string $units, string $exclude)
参数
- 纬度
- 经度
- 单位(英制(默认),公制或开尔文)
- 排除(可选的逗号分隔值:current,hourly,daily)
一次性历史请求方法
getHistoricalWeatherByCoords(string $latitude, string $longitude, string $date, string $units)
参数
- 纬度
- 经度
- 日期(例如:7/12/2020)
- 单位(英制(默认),公制或开尔文)