stuarttodd-dev / weather-forecast-app
一个用于根据IP地址和时间戳获取天气预报的软件包。
dev-main
2023-09-21 16:26 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2024-09-21 18:34:24 UTC
README
PHP天气预报库是一个PHP软件包,它基于地理位置数据提供天气预报信息。它使用外部服务来获取地理位置和天气预报数据。
安装
从Open Weather API获取API密钥。访问https://openweathermap.org/api/one-call-3 获取更多信息。
您可以通过Composer安装此库。运行以下命令
composer require stuarttodd-dev/weather-forecast-app
配置composer.json
软件包仍在开发中。
"minimum-stability": "dev",
静态使用
use StuartToddDev\WeatherForecast\Services\WeatherForecast;
$weatherForecast = WeatherForecast::create('your-ip-address', 'your-open-weather-api-key');
$weatherForecast->getIpAddress(); // returns ip
$weatherForecast->getGeolocation(); // e.g {"status":"success","country":"United Kingdom","countryCode":"GB","region":"ENG","regionName":"England","city":"Stockton-on-Tees","zip":"TS18","lat":54.5579,"lon":-1.328,"timezone":"Europe/London","isp":"Virgin Media","org":"","as":"AS5089 Virgin Media Limited","query":"86.22.157.53"}
$weatherForecast->getForecast(); // Json string from open weather API (see https://openweathermap.org/api/one-call-3)
注入API服务
use StuartToddDev\WeatherForecast\Services\WeatherForecast;
use StuartToddDev\WeatherForecast\Services\GeolocationIPAPI;
use StuartToddDev\WeatherForecast\Services\OpenWeatherAPI;
// Initialize the geolocation and weather forecast services
$geolocationAPI = new GeolocationIPAPI(new Client()); // first param is Guzzle client
$forecastAPI = new OpenWeatherAPI(new Client()'); // first param is Guzzle client
You can inject different API services (or adjust them) as long as they implement the relevant contracts (noted below).
// Initialize the WeatherForecast class
$weatherForecast = new WeatherForecast('your-ip-address', 'your-open-weather-api-key', $geolocationAPI, $forecastAPI);
$weatherForecast->getIpAddress(); // returns ip
$weatherForecast->getGeolocation(); // e.g {"status":"success","country":"United Kingdom","countryCode":"GB","region":"ENG","regionName":"England","city":"Stockton-on-Tees","zip":"TS18","lat":54.5579,"lon":-1.328,"timezone":"Europe/London","isp":"Virgin Media","org":"","as":"AS5089 Virgin Media Limited","query":"86.22.157.53"}
$weatherForecast->getForecast(); // Json string from open weather API (see https://openweathermap.org/api/one-call-3)
ForecastContract
由OpenWeatherAPI使用
public function setLon(string $lon): self;
public function getLon(): string;
public function setLat(string $lat): self;
public function getLat(): string;
public function setApiKey(string $apiKey): self;
public function getForecast(): ?string;
GeolocationContract
由GeolocationIPAPI使用
interface GeolocationContract
{
public function setIPAddress(string $ipAddress): self;
public function getIPAddress(): ?string;
public function getGeoLocation(): ?string;
}