rawaby88/open-weather-laravel

Laravel 包,用于提供 Open Weather Map API 集成

1.0.1 2021-03-13 18:05 UTC

This package is auto-updated.

Last update: 2024-09-14 01:14:39 UTC


README

Latest Version on Packagist Scrutinizer Code Quality Build Status Code Intelligence Status GitHub license

🌡 Laravel 包,用于提供 Open Weather map API 集成。

Star ⭐ 仓库以表示支持 🍺

要使用此包,您需要注册 Open Weather Map 服务并生成一个 API 密钥。更多信息请访问: https://home.openweathermap.org/api_keys

安装

使用 composer 需要此包

composer require rawaby88/open-weather-laravel

添加 Service Provider & Facade

对于 Laravel 5.5+

添加包后,服务提供者和外观将被自动发现。

对于较旧的 Laravel 版本

将 ServiceProvider 添加到 config/app.php 文件中的 providers 数组

Rawaby88\OpenWeatherMap\Providers\OpenWeatherServiceProvider::class,

发布配置

完成之后,使用以下命令将配置发布到您的配置文件夹:

php artisan vendor:publish --provider="Rawaby88\OpenWeatherMap\Providers\OpenWeatherServiceProvider"

配置

配置文件发布后,打开 config/open-weather.php

全局配置

api_token
您的 API 密钥放在这里。或者将其添加到您的 .env 文件 WEATHER_API_TOKEN

api_icon_url
显示从 API 发送的图标或创建您自己的图标

unit
温度可用温度单位为华氏度、摄氏度和开尔文。

language
天气描述语言。

用法

调用一个位置的当前天气数据

按城市名称

您可以通过城市名称或城市名称和国家代码,或城市名称、国家州代码进行调用。请注意,仅限美国位置可通过州代码搜索。

use Rawaby88\OpenWeatherMap\Services\CWByCityName;

$cw = (new CWByCityName('London'))->get();
/**
 * With ISO 3166 country codes 
 * $cw = (new CWByCityName('Warsaw', 'pl'))->get();
 */

按城市 ID

您可以通过城市 ID 进行 API 调用。城市 ID 列表 'city.list.json.gz' 可以在此处下载

use Rawaby88\OpenWeatherMap\Services\CWByCityId;

$cw = (new CWByCityId(2172797))->get();

按地理坐标

您可以通过 ZIP 码进行 API 调用。请注意,如果未指定国家,则默认搜索 USA。

use Rawaby88\OpenWeatherMap\Services\CWByZipCode;

$cw = (new CWByZipCode(94040, 'us'))->get();

按 ZIP 码

您可以通过纬度和经度坐标进行调用

use Rawaby88\OpenWeatherMap\Services\CWByCoordinates;

// CWByCoordinates(latitude, longitude)
$cw = (new CWByCoordinates(35, 139))->get();

单结果示例

use Rawaby88\OpenWeatherMap\Services\CWByCityName;

$cw = (new CWByCityName('London'))->get();
/**
 * With ISO 3166 country codes 
 * $cw = (new CWByCityName('Warsaw', 'pl'))->get();
 */

$temperature = $cw->temperature; // return Temperature object
$weather     = $cw->weather;     // return Weather object 
$location    = $cw->location;    // return Location object  
$sun         = $cw->sun;         // return Sun object

/** 
 * Each one of [temperature, weather, location, sun]
 * can be encoded to json by calling toJson();
 */
$temperature->toJson();
$weather->toJson();
$location->toJson();
$sun->toJson();

调用多个城市的当前天气数据

矩形区域内的城市

API 返回由地理坐标定义的矩形内城市的数据。

use Rawaby88\OpenWeatherMap\Services\CWByRectangleZone;

//CWByRectangleZone(lon-left,lat-bottom,lon-right,lat-top,zoom)
$cw = (new CWByRectangleZone(12,32,15,37,10))->get();

矩形区域内的城市

API 返回由地理坐标定义的矩形内城市的数据。

use Rawaby88\OpenWeatherMap\Services\CWByCitiesInCircle;

//CWByCitiesInCircle(latitude, longitude, number_of cities_to_return)
$cw = (new CWByCitiesInCircle(55.5, 37.5, 10))->get();

多城市示例

use Rawaby88\OpenWeatherMap\Services\CWByCitiesInCircle;

$cw = (new CWByCitiesInCircle(55.5, 37.5, 10))->get();

//to loop through cities result
foreach ($cw->list as $city)
{
    $temperature = $city->temperature; // return Temperature object
    $weather     = $city->weather;     // return Weather object 
    $location    = $city->location;    // return Location object  
    $sun         = $city->sun;         // return Sun object
}

//get specific city with index
$city1 = $cw->index(2);

//get cities count in the list
$city1 = $cw->count();

//get the first city in the list
$city1 = $cw->first();

温度对象

包含温度信息

use Rawaby88\OpenWeatherMap\Services\CWByCityName;

$cw = (new CWByCityName('London'))->get();
$temperature = $cw->temperature;

$temperature->temp; //return current temperature
$temperature->feelsLike; //return feels like
$temperature->tempMax; //return max temperature
$temperature->tempMin; //return min temperature
$temperature->pressure; //return pressure
$temperature->humidity; //return humidity

/**
 * toJson()
 * {"temp":9,"feelsLike":4,"tempMax":10,"tempMin":9,"pressure":1000,"humidity":53}
 */ 
$temperature->toJson();

天气对象

包含天气信息

use Rawaby88\OpenWeatherMap\Services\CWByCityName;

$cw = (new CWByCityName('London'))->get();
$weather = $cw->weather;

$weather->condition; //return current condition
$weather->description; //return weather description
$weather->icon; //return weather icon
$weather->iconUrl; //return icon url
$weather->windSpeed; //return wind speed
$weather->windDeg; //return wind degree
$weather->windDir; //return wind direction
$weather->clouds; //return clouds
$weather->precipitationVolume; //return rain volume
$weather->snowVolume; //return snow volume
                
/**
 * toJson()
 * {"condition":"Clouds","description":"broken clouds","icon":"04d","iconUrl":"\/\/openweathermap.org\/img\/w\/04d.png","windSpeed":5.14,"windDeg":210,"windDir":"SSW","clouds":75,"precipitationVolume":[],"snowVolume":[]} 
 */
$weather->toJson();

太阳对象

包含太阳信息

use Rawaby88\OpenWeatherMap\Services\CWByCityName;

$cw = (new CWByCityName('London'))->get();
$sun = $cw->sun;

$sun->sunrise; //return sunrise
$sun->sunset; //return sunset
$sun->timezone; //return timezone

/**
 * toJson()
 * {"sunrise":{"date":"2021-03-13 05:54:53.000000","timezone_type":1,"timezone":"+00:00"},"sunset":{"date":"2021-03-13 17:36:07.000000","timezone_type":1,"timezone":"+00:00"},"timezone":3600}
 */ 
$sun->toJson();

位置对象

包含位置信息

use Rawaby88\OpenWeatherMap\Services\CWByCityName;

$cw = (new CWByCityName('London'))->get();
$location = $cw->location;

$location->city; //return city name
$location->countryCode; //return ISO country code
$location->lon; //return longitude coordinate
$location->lat; //return latitude coordinate

/**
 * toJson()
 * {"city":"Warsaw","countryCode":"PL","lon":21.0118,"lat":52.2298}
 */ 
$location->toJson();

参数 unit & language

天气参数 unit

use Rawaby88\OpenWeatherMap\Services\CWByCityName;

$cw = new CWByCityName('London');

/**
 * You can change the unit type before making the call.
 * The default value will be called from config file if there was no changes.
 */
$cw->setUnitType('imperial');

/**
 * You can also change the language before making the call
 * The default value will be called from config file if there was no changes.
 */ 
$cw->setLanguage('pl');

$cw->volUnit; // rain and snow volume unit | mm
$cw->presUnit; // pressure unit | hPa
$cw->distUnit; //Distance unit meter/sec | miles/hour
$cw->tempUnit; //Temperature unit Kelvin | Celsius | Fahrenheit.


$cw->get();

贡献

欢迎拉取请求。对于重大更改,请先打开一个问题来讨论您想要更改的内容。

请确保适当更新测试。

变更日志

请参阅 CHANGELOG 了解最近更改的更多信息。

鸣谢

许可

MIT MIT 许可证。有关更多信息,请参阅 许可文件