scrnr/weather

PHP中的OpenWeatherApi应用

v1.0.0 2023-04-27 17:41 UTC

This package is auto-updated.

Last update: 2024-09-28 14:46:57 UTC


README

PHP Version Packagist Version LICENSE

目录

描述

此存储库是用PHP编写的,允许您使用OpenWeatherMap API 2.5检查特定位置的天气。该应用程序可以通过IP地址确定您的位置并显示天气信息,或者您可以输入任何城市的名称。

安装

您可以使用Composer安装库。只需将以下行添加到您的composer.json文件中,然后运行composer install

"require": {
    "scrnr/weather": "*"
}

或者您可以使用以下命令

composer require scrnr/weather

如何使用

要使用此应用程序,您需要从OpenWeatherMap网站获取一个API密钥并使用setToken()函数设置它。该setToken()函数接受一个字符串格式的令牌。

设置您的API密钥后,您可以使用getWeather()函数检索天气数据。此函数接受两个参数:CITY_NAMEUNITS_OF_MEASUREMENT

  • CITY_NAME接受字符串格式的城市名称
  • UNITS_OF_MEASUREMENT接受Units::METRICUnits::IMPERIAL作为常量来指定测量单位。

如果您不设置这些参数,函数将自动根据IP地址确定城市并使用公制系统作为默认测量单位。

如果getWeather()函数返回false,您可以使用getErrors()函数来检索有关错误的信息。这将允许您排查在处理过程中可能发生的任何问题。

感谢您使用我的OpenWeatherMap API应用程序!

例如

use Scrnr\Weather\Weather;

Weather::setToken('YOUR_TOKEN');
$forecast = Weather::getWeather('CITY_NAME', 'UNITS');

if ($forecast === false) {
    $errors = Weather::getErrors();
}

输出

Array
(
    [today] => Array
        (
            [sunrise] => 05:09
            [sunset] => 20:43
            [date] => 27 April
            [description] => Overcast clouds
            [temp] => 5
            [feelsLike] => 1
            [pressure] => 755 mm Hg
            [humidity] => 90%
            [cityName] => Saint Petersburg
            [visibility] => more than 10 km
            [wind] => Array
                (
                    [speed] => 7 m/s
                    [direction] => East
                )

            [isNight] => false
            [icon] => http://openweathermap.org/img/wn/04d@2x.png
            [tempMin] => 4
        )
        
    [fourDays] => Array
        (
            [0] => Array
                (
                    [date] => 28 April
                    [icon] => http://openweathermap.org/img/wn/04d@2x.png
                    [max] => 7
                    [min] => 3
                )

            [1] => Array
                (
                    [date] => 29 April
                    [icon] => http://openweathermap.org/img/wn/10d@2x.png
                    [max] => 4
                    [min] => 4
                )

            [2] => Array
                (
                    [date] => 30 April
                    [icon] => http://openweathermap.org/img/wn/10d@2x.png
                    [max] => 7
                    [min] => 4
                )

            [3] => Array
                (
                    [date] => 01 May
                    [icon] => http://openweathermap.org/img/wn/10d@2x.png
                    [max] => 7
                    [min] => 2
                )
        )
)

功能

此应用程序提供以下功能

  • 检查特定城市的天气
  • 使用IP地址自动定位
  • 显示当前天气状况、温度、湿度和风速
  • 显示4天的天气预报

需求

此应用程序需要PHP 8.0或更高版本,并在您的Web服务器上安装cURLjson扩展。

作者

👤 GitHub: scrnr

许可证

本项目采用MIT许可证。有关详细信息,请参阅LICENSE文件。