coliving / laravel-owm
cmfcmf/openweathermap-php-api 的包装器。此包从 gmopx/laravel-owm 分支出来,因为它已被放弃
Requires
- php: >=7.2.0
- cmfcmf/openweathermap-php-api: ^2.1
This package is not auto-updated.
Last update: 2024-09-17 23:55:19 UTC
README
OpenWeatherMap-PHP-Api 的包装器,由 @cmfcmf 编写
此包允许您在 Laravel 项目中以 Laravel 方式实现 OpenWeatherMap-PHP-Api。
1. 安装
composer require coliving/laravel-owm
2. 在您的 conf/app.php 文件中添加此行
对于 Laravel == 5.0.*
'Gmopx\LaravelOWM\LaravelOWMServiceProvider'
对于 Laravel <= 5.4.*
Gmopx\LaravelOWM\LaravelOWMServiceProvider::class,
对于 Laravel >= 5.5.* 将使用自动发现功能。
3. 发布配置文件 (config/laravel-owm.php)
php artisan vendor:publish --provider="Gmopx\LaravelOWM\LaravelOWMServiceProvider"
4. 添加您的 Open Weather Map API 密钥
...
'api_key' => '', // visit: http://openweathermap.org/appid#get for more info.
'routes_enabled' => true, // If the routes have to be enabled.
...
如何使用...
当前天气
...
use Gmopx\LaravelOWM\LaravelOWM;
...
public function foo()
{
$lowm = new LaravelOWM();
$current_weather = $lowm->getCurrentWeather('london');
dd($current_weather->temperature);
}
有关更多信息,请访问 https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/CurrentWeather.php
预报
...
use Gmopx\LaravelOWM\LaravelOWM;
...
public function bar()
{
$lowm = new LaravelOWM();
$forecast = $lowm->getWeatherForecast('london');
dd($forecast);
}
有关更多信息,请访问 https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php
历史记录
...
use Gmopx\LaravelOWM\LaravelOWM;
...
public function bar()
{
$lowm = new LaravelOWM();
// Get yesterday's date
$date = new \DateTime();
$date->add(\DateInterval::createFromDateString('yesterday'));
$history = $lowm->getWeatherHistory('london', $date);
dd($history);
}
有关更多信息,请访问 https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php
参数
注意
There are three ways to specify the place to get weather information for:
- Use the city name: $query must be a string containing the city name.
- Use the city id: $query must be an integer containing the city id.
- Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
getCurrentWeather
/**
* Get the current weather of the requested location/city.
*
* More info about how to interact with the results:
*
* https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/CurrentWeather.php
*
* @param $query (required)
* @param string $lang (default: en) - http://openweathermap.org/current#multi.
* @param string $units (default: metric) - 'metric' or 'imperial'.
* @param bool $cache (default: false)
* @param int $time (default: 600)
* @return OpenWeatherMap\CurrentWeather
*/
public function getCurrentWeather($query, $lang = 'en', $units = 'metric', $cache = false, $time = 600)
...
getWeatherForecast
/**
* Get the forecast of the requested location/city.
*
* More info about how to interact with the results:
*
* https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php
*
* @param $query (required)
* @param string $lang (default: en) - http://openweathermap.org/current#multi.
* @param string $units (default: metric) - 'metric' or 'imperial'.
* @param int $days (default: 5) - maximum 16.
* @param bool $cache (default: false)
* @param int $time (default: 600)
* @return OpenWeatherMap\WeatherForecast
*/
public function getWeatherForecast($query, $lang = 'en', $units = 'metric', $days = 5, $cache = false, $time = 600)
...
getDailyWeatherForecast
/**
* Get the daily forecast of the requested location/city.
*
*
* There are three ways to specify the place to get weather information for:
* - Use the city name: $query must be a string containing the city name.
* - Use the city id: $query must be an integer containing the city id.
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* @param array|int|string $query
* @param string $lang
* @param string $units
* @param int $days
* @param bool $cache
* @param int $time
* @return OpenWeatherMap\WeatherForecast
*/
public function getDailyWeatherForecast($query, $lang = 'en', $units = 'metric', $days = 5, $cache = false, $time = 600)
...
getWeatherHistory
/**
* Get the forecast of the requested location/city.
*
* More info about how to interact with the results:
*
* https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherHistory.php
*
* @param $query (required)
* @param \DateTime $start (default: today)
* @param int $endOrCount (default: 1)
* @param string $type (default: hour) - 'tick', 'hour', or 'day'
* @param string $lang (default: en) - http://openweathermap.org/current#multi.
* @param string $units (default: metric) - 'metric' or 'imperial'.
* @param bool $cache (default: false)
* @param int $time (default: 600)
* @return OpenWeatherMap\WeatherForecast
*/
public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $lang = 'en', $units = 'metric', $cache = false, $time = 600)
...
路由
此外,此包还包括 2 个可用于在 JSON 格式中显示天气数据的路由。
[GET]
/current-weather
参数
city: 'London'
coord: ['lat': -0.13, 'lon': 51.51]
lang: 'en' (default: 'en')
units: 'metric'||'imperial' (default: 'metric')
[GET]
/forecast
参数
city: 'London'
coord: ['lat': -0.13, 'lon': 51.51]
lang: 'en' (default: 'en')
units: 'metric'||'imperial' (default: 'metric')
days: 5 (default: 5)
注意:您必须使用 city
或 coord
,但不能同时使用两者。
/current-weather
请考虑以下
- 结果日期的时间区域是从 'app/config.php' 文件 ('app.timezone') 中获取的。
{
"status":"ok",
"data":{
"city":{
"id":2643743,
"name":"London",
"lat":51.51,
"lon":-0.13,
"country":"GB",
"population":null
},
"sun":{
"rise":{
"date":"2016-09-14 05:34:37",
"timestamp":1473831277
},
"set":{
"date":"2016-09-14 18:16:08",
"timestamp":1473876968
}
},
"lastUpdate":{
"date":"2016-09-14 03:58:17",
"timestamp":1473825497
},
"temperature":{
"now":{
"value":19.39,
"unit":"°C"
},
"min":{
"value":17,
"unit":"°C"
},
"max":{
"value":21.67,
"unit":"°C"
}
},
"humidity":{
"value":82,
"unit":"%"
},
"pressure":{
"value":1008,
"unit":"hPa"
},
"wind":{
"speed":{
"value":2.1,
"unit":"m/s",
"description":"Light breeze",
"description_slug":"light-breeze"
},
"direction":{
"value":100,
"unit":"E",
"description":"East",
"description_slug":"east"
}
},
"clouds":{
"value":8,
"unit":"",
"description":"clear sky",
"description_slug":"clear-sky"
},
"precipitation":{
"value":0,
"unit":"",
"description":"no",
"description_slug":"no"
},
"weather":{
"id":800,
"description":"clear sky",
"description_slug":"clear-sky",
"icon":"02n"
}
}
}
/forecast (5 天)
请考虑以下
- 结果日期的时间区域是从 'app/config.php' 文件 ('app.timezone') 中获取的。
- 天数数组的索引是 ISO-8601 数字表示的星期几,1(周一)到 7(周日)。
- OWM API 返回 3 小时预报数据,这意味着对于您请求的每一天,您将按以下顺序每 3 小时获取一次天气数据:06:00 - 09:00,09:00 - 12:00,12:00 - 15:00,15:00 - 18:00,18:00 - 21:00,21:00 - 00:00。因此,为了保持良好的顺序,我创建了一个基于小时范围的键(例如:['06-09'] : { ... })。
{
"status":"ok",
"data":{
"city":{
"id":0,
"name":"London",
"lat":51.50853,
"lon":-0.12574,
"country":"GB",
"population":null
},
"sun":{
"rise":{
"date":"2016-09-14 05:34:37",
"timestamp":1473831277
},
"set":{
"date":"2016-09-14 18:16:05",
"timestamp":1473876965
}
},
"lastUpdate":{
"date":"2016-09-14 04:17:23",
"timestamp":1473826643
},
"days":{
"3":{
"06-09":{
"time":{
"from":{
"date":"2016-09-14 06:00:00",
"timestamp":1473832800
},
"to":{
"date":"2016-09-14 09:00:00",
"timestamp":1473843600
},
"day":{
"date":"2016-09-14 00:00:00",
"timestamp":1473811200
}
},
"lastUpdate":{
"date":"2016-09-14 04:17:23",
"timestamp":1473826643
},
"temperature":{
"now":{
"value":25.45,
"unit":"°C"
},
"min":{
"value":25.18,
"unit":"°C"
},
"max":{
"value":25.72,
"unit":"°C"
}
},
"humidity":{
"value":53,
"unit":"%"
},
"pressure":{
"value":1016.12,
"unit":"hPa"
},
"wind":{
"speed":{
"value":3.12,
"unit":"m\/s",
"description":"Light breeze",
"description_slug":"light-breeze"
},
"direction":{
"value":111.003,
"unit":"ESE",
"description":"East-southeast",
"description_slug":"east-southeast"
}
},
"clouds":{
"value":0,
"unit":"%",
"description":"clear sky",
"description_slug":"clear-sky"
},
"precipitation":{
"value":0,
"unit":"",
"description":"",
"description_slug":""
},
"weather":{
"id":800,
"description":"clear sky",
"description_slug":"clear-sky",
"icon":"01d"
}
},
...
},
"4":{
"03-06":{
"time":{
"from":{
"date":"2016-09-15 03:00:00",
"timestamp":1473908400
},
"to":{
"date":"2016-09-15 06:00:00",
"timestamp":1473919200
},
"day":{
"date":"2016-09-15 00:00:00",
"timestamp":1473897600
}
},
"lastUpdate":{
"date":"2016-09-14 04:17:23",
"timestamp":1473826643
},
"temperature":{
"now":{
"value":16.34,
"unit":"°C"
},
"min":{
"value":16.34,
"unit":"°C"
},
"max":{
"value":16.34,
"unit":"°C"
}
},
"humidity":{
"value":82,
"unit":"%"
},
"pressure":{
"value":1015.58,
"unit":"hPa"
},
"wind":{
"speed":{
"value":1.22,
"unit":"m\/s",
"description":"Calm",
"description_slug":"calm"
},
"direction":{
"value":40.0064,
"unit":"NE",
"description":"NorthEast",
"description_slug":"northeast"
}
},
"clouds":{
"value":8,
"unit":"%",
"description":"clear sky",
"description_slug":"clear-sky"
},
"precipitation":{
"value":0,
"unit":"",
"description":"",
"description_slug":""
},
"weather":{
"id":800,
"description":"clear sky",
"description_slug":"clear-sky",
"icon":"02d"
}
},
...
},
"5":{
"03-06":{
"time":{
"from":{
"date":"2016-09-16 03:00:00",
"timestamp":1473994800
},
"to":{
"date":"2016-09-16 06:00:00",
"timestamp":1474005600
},
"day":{
"date":"2016-09-16 00:00:00",
"timestamp":1473984000
}
},
"lastUpdate":{
"date":"2016-09-14 04:17:23",
"timestamp":1473826643
},
"temperature":{
"now":{
"value":17,
"unit":"°C"
},
"min":{
"value":17,
"unit":"°C"
},
"max":{
"value":17,
"unit":"°C"
}
},
"humidity":{
"value":87,
"unit":"%"
},
"pressure":{
"value":1018.92,
"unit":"hPa"
},
"wind":{
"speed":{
"value":2.81,
"unit":"m\/s",
"description":"Light breeze",
"description_slug":"light-breeze"
},
"direction":{
"value":314.505,
"unit":"NW",
"description":"Northwest",
"description_slug":"northwest"
}
},
"clouds":{
"value":88,
"unit":"%",
"description":"overcast clouds",
"description_slug":"overcast-clouds"
},
"precipitation":{
"value":0,
"unit":"",
"description":"",
"description_slug":""
},
"weather":{
"id":804,
"description":"overcast clouds",
"description_slug":"overcast-clouds",
"icon":"04d"
}
},
...
},
"6":{
"03-06":{
"time":{
"from":{
"date":"2016-09-17 03:00:00",
"timestamp":1474081200
},
"to":{
"date":"2016-09-17 06:00:00",
"timestamp":1474092000
},
"day":{
"date":"2016-09-17 00:00:00",
"timestamp":1474070400
}
},
"lastUpdate":{
"date":"2016-09-14 04:17:23",
"timestamp":1473826643
},
"temperature":{
"now":{
"value":11.53,
"unit":"°C"
},
"min":{
"value":11.53,
"unit":"°C"
},
"max":{
"value":11.53,
"unit":"°C"
}
},
"humidity":{
"value":91,
"unit":"%"
},
"pressure":{
"value":1029.85,
"unit":"hPa"
},
"wind":{
"speed":{
"value":2.91,
"unit":"m\/s",
"description":"Light breeze",
"description_slug":"light-breeze"
},
"direction":{
"value":314.505,
"unit":"NW",
"description":"Northwest",
"description_slug":"northwest"
}
},
"clouds":{
"value":36,
"unit":"%",
"description":"scattered clouds",
"description_slug":"scattered-clouds"
},
"precipitation":{
"value":0,
"unit":"",
"description":"",
"description_slug":""
},
"weather":{
"id":802,
"description":"scattered clouds",
"description_slug":"scattered-clouds",
"icon":"03d"
}
},
...
},
"7":{
"03-06":{
"time":{
"from":{
"date":"2016-09-18 03:00:00",
"timestamp":1474167600
},
"to":{
"date":"2016-09-18 06:00:00",
"timestamp":1474178400
},
"day":{
"date":"2016-09-18 00:00:00",
"timestamp":1474156800
}
},
"lastUpdate":{
"date":"2016-09-14 04:17:23",
"timestamp":1473826643
},
"temperature":{
"now":{
"value":11.44,
"unit":"°C"
},
"min":{
"value":11.44,
"unit":"°C"
},
"max":{
"value":11.44,
"unit":"°C"
}
},
"humidity":{
"value":98,
"unit":"%"
},
"pressure":{
"value":1031.08,
"unit":"hPa"
},
"wind":{
"speed":{
"value":2.96,
"unit":"m\/s",
"description":"Light breeze",
"description_slug":"light-breeze"
},
"direction":{
"value":240.011,
"unit":"WSW",
"description":"West-southwest",
"description_slug":"west-southwest"
}
},
"clouds":{
"value":0,
"unit":"%",
"description":"clear sky",
"description_slug":"clear-sky"
},
"precipitation":{
"value":0,
"unit":"",
"description":"",
"description_slug":""
},
"weather":{
"id":800,
"description":"clear sky",
"description_slug":"clear-sky",
"icon":"01d"
}
},
...
}
}
}
}
变更日志
v0.1.2
- 支持 getDailyWeatherForecast
v0.1.1
- 空白/格式化。
- 添加 getWeatherHistory 方法
- 额外的 README 文档
感谢
许可:MIT
贡献
您可以帮助我修复文档和注释中的糟糕的英语。