Created
February 28, 2024 12:36
-
-
Save AAKempf/f9db73067c1db801ac90f17e32e441bb to your computer and use it in GitHub Desktop.
Example class for weather forecast with php-weather/open-meteo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use Http\Adapter\Guzzle7\Client; | |
use PhpWeather\Common\WeatherQuery; | |
use PhpWeather\Exception; | |
use PhpWeather\Provider\OpenMeteo\OpenMeteo as myWeatherData; | |
use PhpWeather\Weather; | |
/** | |
* Example class for weather forecast with php-weather/open-meteo | |
* Shows the connection and two demo functions | |
* | |
* @see https://github.com/php-weather | |
*/ | |
class WeatherData | |
{ | |
private static $httpClient; | |
private static $weatherData; | |
private float $latitude; | |
private float $longitude; | |
private array $forecastData; | |
public function __construct(float $latitude, float $longitude) | |
{ | |
$this->latitude = $latitude; | |
$this->longitude = $longitude; | |
$this->forecastData = $this->getForecast(); | |
} | |
/** | |
* @return Weather[] | |
*/ | |
protected function getForecast():array | |
{ | |
try { | |
$currentWeatherQuery = WeatherQuery::create($this->latitude, $this->longitude); | |
return $this->getWeatherData()->getForecast($currentWeatherQuery)->getForecast(); | |
} catch (Exception $e) { | |
echo "Exception: " . $e->getMessage(); | |
} | |
return []; | |
} | |
/** | |
* @return object | |
*/ | |
private function getWeatherData():object | |
{ | |
if (!self::$httpClient) { | |
self::$httpClient = new Client(); | |
} | |
if (!self::$weatherData) { | |
// The "Provider" line can be replaced with another weather service, f.e: | |
// use PhpWeather\Provider\Tomorrow\Tomorrow as myWeatherData; | |
self::$weatherData = new myWeatherData(self::$httpClient); | |
} | |
return self::$weatherData; | |
} | |
/** | |
* @return string | |
*/ | |
public function listMinMaxTemperaturesByDate(): string | |
{ | |
[$minTemps, $maxTemps] = $this->getMinMaxTemperaturesByDate(); | |
foreach ($minTemps as $date => $temp) { | |
$ret[] = "On $date, min $temp and max " . $maxTemps[$date]; | |
} | |
return implode('<br />', $ret ?? []); | |
} | |
/** | |
* @return string | |
*/ | |
protected function getMinMaxTemperaturesByDate(): array | |
{ | |
$minTemps = []; | |
$maxTemps = []; | |
foreach ($this->forecastData as $obj) { | |
$datetime = $obj->getUtcDateTime(); | |
if ($datetime !== null) { // Verify datetime is not null | |
$date = $datetime->format('Y-m-d'); | |
$temp = $obj->getTemperature(); | |
$minTemps[$date] = isset($minTemps[$date]) ? min($minTemps[$date], $temp) : $temp; | |
$maxTemps[$date] = isset($maxTemps[$date]) ? max($maxTemps[$date], $temp) : $temp; | |
} | |
} | |
return [$minTemps, $maxTemps]; | |
} | |
/** | |
* @return array[] | |
*/ | |
public function getByDate(string $setTime = ''): array | |
{ | |
$targetDateTime = !empty($setTime) ? $setTime : date('Y-m-d H:00:00'); | |
foreach ($this->forecastData as $obj) { | |
$datetime = $obj->getUtcDateTime(); | |
$format = $datetime !== null ? $datetime->format('Y-m-d H:i:s') : ''; | |
if ($format === $targetDateTime) { | |
$forecastPerDate['Icon'] = $obj->getIcon(); | |
$forecastPerDate['Temperature'] = $obj->getTemperature(); | |
$forecastPerDate['FeelsLike'] = $obj->getFeelsLike(); | |
$forecastPerDate['WindDirection'] = $obj->getWindDirection(); | |
$forecastPerDate['WindSpeed'] = $obj->getWindSpeed(); | |
// and so on... | |
break; | |
} | |
} | |
return $forecastPerDate ?? []; | |
} | |
} | |
// Usage: | |
$latitude = 53.5; | |
$longitude = 10; | |
$weatherData = new WeatherData($latitude, $longitude); | |
echo $weatherData->listMinMaxTemperaturesByDate(); | |
echo '<hr />'; | |
echo implode('<br />', $weatherData->getByDate()); | |
echo '<hr />'; | |
$forecastDate = date('Y-m-d H:00:00', strtotime('12 hours')); | |
echo implode('<br />', $weatherData->getByDate($forecastDate)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment