paule / weather-by-ip
一个通过IP获取天气的包
README
此Laravel包允许从提供的或识别的用户IP地址获取五天的天气预报。这可以通过网页视图、外观或通过CLI的artisan命令来完成。
安装与设置
由于该包托管在Packagist上,您可以通过composer安装该包
composer require paule/weather-by-ip
该包将自动使用Laravel的包发现功能注册自己。
您还需要使用以下命令将包的资源、配置和语言文件发布到您的应用程序文件系统中
php artisan vendor:publish --provider="PaulE\WeatherByIP\WeatherByIPServiceProvider"
如果不这样做,网页视图将无法正确加载样式和资源。
之后,请运行以下命令
php artisan migrate
这将设置包的数据库表。如果您希望自定义表名,请参阅下面的配置信息
该包的配置文件默认包含以下内容
return [ 'url_prefix' => 'weatherbyip', 'middleware' => ['web'], 'http_driver' => 'guzzle', // Set above to anything but guzzle to use cURL 'tables' => [ 'location_lookups' => 'location_lookups', 'forecasts' => 'forecasts' ], ];
如果需要,可以覆盖 url_prefix
、http_driver
和 tables
的值来轻松调整包的功能,以及根据需要覆盖网页视图路由的 middleware
。
用法
该包为功能提供了三种访问路由
外观
该包提供了一个外观,您可以使用它来访问包的功能。外观的类名为 WeatherByIP
,其别名为 WeatherByIP
。
获取位置/预报信息的函数为 getForecast(array $payload)
,需要以以下模式声明带有数组输入的函数
$output = WeatherByIP::getForecast(['ip'=> '212.49.244.121','datetime' => '2017-06-01 17:30:00']);
这将返回错误,格式如下
['error' => 'Error message']
如果请求成功,将返回格式,例如
{"ip":"123.211.61.50","location":{"id":11,"location":"Brisbane, Queensland, AU","lookup_time":"2023-09-01 12:10:18"},"forecast":[{"forecast_date":"2023-09-01","weather_code":45,"temp_high":26.9,"temp_low":15.3,"conditions":"Foggy"},{"forecast_date":"2023-09-02","weather_code":2,"temp_high":26.1,"temp_low":14.6,"conditions":"Partly Cloudy"},{"forecast_date":"2023-09-03","weather_code":45,"temp_high":28.7,"temp_low":12.6,"conditions":"Foggy"},{"forecast_date":"2023-09-04","weather_code":45,"temp_high":26.7,"temp_low":15,"conditions":"Foggy"},{"forecast_date":"2023-09-05","weather_code":3,"temp_high":26.7,"temp_low":17.4,"conditions":"Cloudy"}]}
网页视图
该包还提供了一个可以访问的网页视图,默认情况下位于 /weatherbyip/forecast
路径。如果更改了 url_prefix
配置,则相应地更改路由。该网页视图允许用户输入IP地址并检索关联位置的即将到来的五天预报。
Artisan命令
该包还提供了一个Artisan命令,您可以使用它从IP地址检索五天的预报。命令的名称为 weatherbyip:forecast
,它有一个条件参数,即IP地址。如果没有提供IP地址,它将检索用户的请求IP(或,如果用户的请求IP是私有/保留的,则作为后备,检索 123.211.61.50
)。
例如,以以下方式运行命令
php artisan weatherbyip:forecast 122.62.248.72
将以以下格式返回
Retrieving Weather By IP... 122.62.248.72: located at Gore, Southland, NZ Weather Forecast: 2023-09-01: Cloudy, Temperature 4.3 - 16.9 C 2023-09-02: Light Rain, Temperature 4.1 - 14.5 C 2023-09-03: Partly Cloudy, Temperature 1.1 - 15 C 2023-09-04: Cloudy, Temperature 5.3 - 18.8 C 2023-09-05: Cloudy, Temperature 11.6 - 21.6 C
您可以通过运行以下命令查看可用选项和参数
php artisan help weatherbyip:forecast
还有一个第二个Artisan命令,weatherbyip:delete
,它将引导用户删除数据库中的所有现有位置查找和预报。
设置
在配置文件中,如果将 http_driver
的值设置为除 guzzle
之外的内容,则包将切换到使用cURL来检索API端点,而不是GuzzleHttp。
同样在配置文件中,默认数据库表名设置在 tables
数组中。这些可以修改为不同的值以确保没有冲突。
当执行 vendor:publish
命令时,图标图像被发布到应用程序的 public/packages/weatherbyip/forecast_icons
文件夹,每个图像的文件名对应于Open Meteo天气代码。如果需要,可以替换这些图像,但必须保持 .png
格式,最好是相同的大小。如果您想了解每个天气代码的含义,建议查看 lang/.../forecasts.php
文件或访问Open Meteo的API文档。
本包包含英语和法语翻译,并在执行 vendor:publish
命令时将语言文件发布到您的应用程序的 lang
文件夹中。如果需要,还可以添加额外的翻译。
致谢
感谢Materialize、Free IP API、Open Meteo和Open Weather Map提供的样式、API端点和图标。