mohamedshuaau / open-weather

简单的 Open Weather API 管理器

dev-master 2020-09-25 13:35 UTC

This package is auto-updated.

Last update: 2024-09-25 23:19:08 UTC


README

此包管理 Open Weather API。

需求

此包需要 Laravel 版本 >= 6.0 和 php 版本 >= 7.2。

安装

使用 composer 下载此包

composer require mohamedshuaau/open-weather

Laravel 的自动发现应该会注册包的提供者。

安装后,您可以使用以下命令发布包内容

php artisan vendor:publish

发布命令会将配置文件发布到 config 文件夹。

更改配置文件的默认值(API 密钥等)。

基本用法

注意:其中一些方法需要从 open weather 购买付费账户,否则可能无法按预期工作。

$weather = Weather::getByGeoCoordinates('39.157711', '21.249649')
                    ->getFiveDayWeatherForecast();

return $weather;

针对不同类型的数据有不同方法可用

//In this example, we will get the current weather
Weather::getByGeoCoordinates('39.157711', '21.249649')
         ->getCurrentWeather();

//You can chain different methods of getting the city/state/country information
//in the above example, the geo location is used which accepts lat and long value

获取城市/州/国家的方法

//Gets the city by name/state code/country code
//params: city name (required), state code (optional), country code (optional)
Weather::getByCityName('New York', '467', 'US');

//Gets the city information by city id
//params: city id (required)
Weather::getByCityId('2377474');

//Gets the city information by geo location coordinates
//params: latitude (required), longitude (required)
Weather::getByGeoCoordinates('12.434333', '34.434555');

//Gets the city information by zip code
//params: zip code (required), country code (optional)
Weather::getByZipCode('34433', '23434444');

天气类型的方法

//with each of these methods, it is mandatory to chain a location method. Eg:
//Weather::getByCityId('2377474')->getCurrentWeather();

//Gets the current weather information
Weather::getCurrentWeather();

//Gets the weather information for the last five days with 3 hours difference in between
//params: limit amount (optional)
Weather::getFiveDayWeatherForecast(3);

//Gets sixteen days weather information with per day information
//params: limit amount (optional)
Weather::getSixteenDayWeatherForecast(3);

//Gets the hourly weather forecast data (requires paid account)
//params: limit amount (optional)
Weather::getHourlyWeatherForecast(3);

//Gets thirty day climate forecast data (requires paid account)
//params: limit amount (optional)
Weather::getThirtyDayClimateForecast(3);

//One Call API
//params: exclude (array optional)
Weather::oneCall(['hour', 'daily']);

//Returns historical weather data
//params: limit amount (optional), type (optional), start date (optional|unix date), end date (optional|unix date)
Weather::historicalWeather(3, 'hour', '23124444214', '234234423423');

//Returns current UV
Weather::CurrentUV();

//Returns Forecast UV
//params: limit amount (optional)
Weather::ForecastUV();

//Returns Historical UV
//params: limit amount (optional), start date (optional|unix date), end date (optional|unix date)
Weather::HistoricalUV(3, '23124444214', '234234423423');

除了发布配置文件中提供的默认配置外,您还可以使用方法链覆盖值

//lets change the language, units and mode
Weather::language('ja')
         ->units('metric')
         ->mode('html')
         ->getByCityId('2377474')
         ->getCurrentWeather();

单位决定了度量单位,模式是返回的数据类型。

可用的单位有

  • metric
  • imperial
  • standard

默认情况下,单位设置为 standard。

可用的模式有

  • html
  • xml
  • json

默认情况下,模式设置为 json。

有关更详细的信息,请访问 Open Weather API 文档

未来还将增加更多功能。此包欢迎建议和改进。
您可自由使用此包并根据您的需求进行修改。