From 3785168e1a665bf9d61d190ea04f20b36c707763 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 8 Jun 2024 09:19:07 -0700 Subject: [PATCH] update to OpenWeathermap 3.0 closes #11 --- p3k/Weather.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/p3k/Weather.php b/p3k/Weather.php index c77c71c..00c0b35 100644 --- a/p3k/Weather.php +++ b/p3k/Weather.php @@ -13,7 +13,7 @@ class Weather { $data = self::_fetch($lat, $lng, $key); if(!$data) return null; - if(!property_exists($data, 'weather')) + if(!property_exists($data, 'current')) return null; $weather = [ @@ -34,39 +34,37 @@ class Weather { ] ]; - $sunny = self::_sunny($data->coord->lat, $data->coord->lon); + $sunny = self::_sunny($data->lat, $data->lon); - $icon_name = self::_icon_name($data->weather[0]->id); + $icon_name = self::_icon_name($data->current->weather[0]->id); $weather['sun'] = $sunny; - $weather['description'] = $data->weather[0]->description; + $weather['description'] = $data->current->weather[0]->description; $weather['icon']['name'] = $icon_name; $weather['temp'] = [ - 'num' => (double)$data->main->temp, + 'num' => (double)$data->current->temp, 'unit' => '°F' ]; $weather['feelslike'] = [ - 'num' => (double)$data->main->feels_like, + 'num' => (double)$data->current->feels_like, 'unit' => '°F' ]; $weather['wind'] = [ - 'num' => $data->wind->speed, + 'num' => $data->current->wind_speed, 'unit' => 'mph' ]; $weather['pressure'] = [ - 'num' => (int)$data->main->pressure, + 'num' => (int)$data->current->pressure, 'unit' => 'mb' ]; $weather['humidity'] = [ - 'num' => round($data->main->humidity), + 'num' => round($data->current->humidity), 'unit' => '%' ]; - $offset = $data->timezone; - - $weather['timezone']['name'] = $sunny['timezone']; - $weather['timezone']['offset'] = $offset; + $weather['timezone']['name'] = $data->timezone; + $weather['timezone']['offset'] = $data->timezone_offset; #$weather['raw'] = $current; @@ -81,7 +79,7 @@ class Weather { 'appid' => $key, ]; $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, 'https://api.openweathermap.org/data/2.5/weather?'.http_build_query($params)); + curl_setopt($ch, CURLOPT_URL, 'https://api.openweathermap.org/data/3.0/onecall?'.http_build_query($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // curl_setopt($ch, CURLOPT_USERAGENT, ''); curl_setopt($ch, CURLOPT_TIMEOUT, 5);