'Italia',
'US' => 'USA',
'GB' => 'Regno Unito',
'FR' => 'Francia',
'DE' => 'Germania',
'ES' => 'Spagna',
'CH' => 'Svizzera'
);
$countryCode = strtoupper(trim((string)$countryCode));
return $map[$countryCode] ?? $countryCode;
}
}
if (!function_exists('meteo_cache_json_with_meta')) {
function meteo_cache_json_with_meta($url, $cacheMinutes = 10) {
$upload = wp_upload_dir();
$cacheDir = trailingslashit($upload['basedir']) . 'meteo-cache';
if (!is_dir($cacheDir)) {
wp_mkdir_p($cacheDir);
}
$cacheFile = trailingslashit($cacheDir) . md5($url) . '.json';
if (file_exists($cacheFile) && filemtime($cacheFile) > (time() - ($cacheMinutes * 60))) {
$raw = @file_get_contents($cacheFile);
$data = json_decode($raw, true);
if (is_array($data)) {
return array(
'data' => $data,
'updatedAt' => filemtime($cacheFile)
);
}
}
$response = false;
if (function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_TIMEOUT => 15,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_USERAGENT => 'Mozilla/5.0 Meteo Widget'
));
$response = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response === false || $httpCode < 200 || $httpCode >= 300) {
$response = false;
}
} else {
$response = @file_get_contents($url);
}
if ($response !== false) {
@file_put_contents($cacheFile, $response);
$data = json_decode($response, true);
if (is_array($data)) {
return array(
'data' => $data,
'updatedAt' => file_exists($cacheFile) ? filemtime($cacheFile) : time()
);
}
}
if (file_exists($cacheFile)) {
$raw = @file_get_contents($cacheFile);
$data = json_decode($raw, true);
if (is_array($data)) {
return array(
'data' => $data,
'updatedAt' => filemtime($cacheFile)
);
}
}
return array(
'data' => null,
'updatedAt' => null
);
}
}
/* =========================
URL API
========================= */
$urlWeatherCom = 'https://api.weather.com/v3/wx/forecast/daily/5day'
. '?geocode=' . urlencode($lat . ',' . $lon)
. '&format=json'
. '&units=m'
. '&language=it-IT'
. '&apiKey=' . urlencode($weatherComApiKey);
$urlOpenWeather = 'https://api.openweathermap.org/data/2.5/forecast'
. '?lat=' . urlencode($lat)
. '&lon=' . urlencode($lon)
. '&lang=it'
. '&units=metric'
. '&appid=' . urlencode($openWeatherApiKey);
/* =========================
CARICO I DATI
========================= */
$weatherComResponse = meteo_cache_json_with_meta($urlWeatherCom, $cacheMinutes);
$openWeatherResponse = meteo_cache_json_with_meta($urlOpenWeather, $cacheMinutes);
$weatherComData = $weatherComResponse['data'];
$openWeatherData = $openWeatherResponse['data'];
$lastUpdatedTs = null;
if (!empty($weatherComResponse['updatedAt']) && !empty($openWeatherResponse['updatedAt'])) {
$lastUpdatedTs = max($weatherComResponse['updatedAt'], $openWeatherResponse['updatedAt']);
} elseif (!empty($weatherComResponse['updatedAt'])) {
$lastUpdatedTs = $weatherComResponse['updatedAt'];
} elseif (!empty($openWeatherResponse['updatedAt'])) {
$lastUpdatedTs = $openWeatherResponse['updatedAt'];
}
$cityName = '';
$countryCode = '';
$locationLabel = '';
if (!empty($openWeatherData['city']['name'])) {
$cityName = $openWeatherData['city']['name'];
}
if (!empty($openWeatherData['city']['country'])) {
$countryCode = $openWeatherData['city']['country'];
}
if ($cityName !== '') {
$locationLabel = $cityName;
if ($countryCode !== '') {
$locationLabel .= ', ' . meteo_country_label($countryCode);
}
}
/* =========================
WEATHER.COM
========================= */
$weatherTabs = array();
if (
is_array($weatherComData) &&
!empty($weatherComData['validTimeLocal']) &&
is_array($weatherComData['validTimeLocal'])
) {
$dayCount = count($weatherComData['validTimeLocal']);
for ($d = 0; $d < $dayCount; $d++) {
$dateYmd = substr((string)$weatherComData['validTimeLocal'][$d], 0, 10);
$weatherTabs[$d] = array(
'date' => $dateYmd,
'label' => meteo_day_label_from_date($dateYmd),
'labelFull' => meteo_day_label_with_date($dateYmd),
'summary' => array(
'dayOfWeek' => $weatherComData['dayOfWeek'][$d] ?? '',
'tempMax' => $weatherComData['temperatureMax'][$d] ?? '-',
'tempMin' => $weatherComData['temperatureMin'][$d] ?? '-',
'qpf' => $weatherComData['qpf'][$d] ?? null,
'qpfSnow' => $weatherComData['qpfSnow'][$d] ?? null,
'sunriseTimeLocal' => $weatherComData['sunriseTimeLocal'][$d] ?? '',
'sunsetTimeLocal' => $weatherComData['sunsetTimeLocal'][$d] ?? '',
'moonPhase' => $weatherComData['moonPhase'][$d] ?? '',
'narrative' => $weatherComData['narrative'][$d] ?? '',
),
'cards' => array()
);
}
if (!empty($weatherComData['daypart'][0]) && is_array($weatherComData['daypart'][0])) {
$parts = $weatherComData['daypart'][0];
if (!empty($parts['daypartName']) && is_array($parts['daypartName'])) {
$currentDayIndex = -1;
for ($i = 0; $i < count($parts['daypartName']); $i++) {
if (empty($parts['iconCode'][$i]) && empty($parts['daypartName'][$i])) {
continue;
}
$name = $parts['daypartName'][$i] ?? '';
$nameLower = function_exists('mb_strtolower') ? mb_strtolower((string)$name) : strtolower((string)$name);
$isNight = (strpos($nameLower, 'sera') !== false || strpos($nameLower, 'notte') !== false);
if (!$isNight) {
$currentDayIndex++;
}
if ($currentDayIndex < 0) {
$currentDayIndex = 0;
}
if (!isset($weatherTabs[$currentDayIndex])) {
continue;
}
$weatherTabs[$currentDayIndex]['cards'][] = array(
'name' => $name,
'partLabel' => $isNight ? 'Notte' : 'Giorno',
'icon' => $parts['iconCode'][$i] ?? '',
'temp' => $parts['temperature'][$i] ?? '-',
'humidity' => $parts['relativeHumidity'][$i] ?? '-',
'precipChance' => $parts['precipChance'][$i] ?? '-',
'windDir' => $parts['windDirectionCardinal'][$i] ?? '-',
'windSpeed' => $parts['windSpeed'][$i] ?? '-',
'windPhrase' => $parts['windPhrase'][$i] ?? '',
'wxPhraseLong' => $parts['wxPhraseLong'][$i] ?? '',
'narrative' => $parts['narrative'][$i] ?? '',
'thunderCategory' => $parts['thunderCategory'][$i] ?? '',
'qpf' => $parts['qpf'][$i] ?? null,
'qpfSnow' => $parts['qpfSnow'][$i] ?? null,
'uvDescription' => $parts['uvDescription'][$i] ?? '',
'uvIndex' => $parts['uvIndex'][$i] ?? '',
);
}
}
}
}
/* =========================
OPENWEATHER
========================= */
$groupedHours = array();
if (!empty($openWeatherData['list']) && is_array($openWeatherData['list'])) {
foreach ($openWeatherData['list'] as $row) {
if (empty($row['dt'])) {
continue;
}
$dayKey = date('Y-m-d', (int)$row['dt']);
if (!isset($groupedHours[$dayKey])) {
$groupedHours[$dayKey] = array();
}
$groupedHours[$dayKey][] = $row;
}
}
?>
Città:
Coordinate: ,
Ultimo aggiornamento:
Previsioni giornaliere da Wunderground
Riepilogo giorno/notte
Pioggia totale: mm
Neve totale: cm
Alba:
Tramonto:
Fase lunare:
°
Condizioni:
Umidità: %
Prob. pioggia: %
Pioggia: mm
Neve: cm
Vento: km/h
Dettaglio vento:
UV:
Temporali:
Note:
Dettagli giorno/notte non disponibili.
Previsioni giornaliere non disponibili.
Previsioni orarie da OpenWeather
Ogni 3 ore
$rows): ?>
$rows): ?>
°
Percepita °
Um. %
Nuvole %
Pioggia %
Rain 3h mm
Snow 3h mm
Press. hPa
km/h
Previsioni orarie non disponibili.