<?php
CURLOPT_URL => 'https://a...content-available-to-author-only...o.com/v1/forecast?latitude=33.8991884&longitude=-118.1862416¤t=temperature_2m&temperature_unit=fahrenheit',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
//var_dump($response);
// Decode JSON response
// Get temperature
// Check if $data is an array and if 'current' exists in $data
$temperature = $data['current'];
// Check if 'temperature_2m' exists in $temperature
if (isset($temperature['temperature_2m'])) { $deg = $temperature['temperature_2m'];
} else {
// Handle case where 'temperature_2m' is not set
$deg = null; // or set a default value
}
// Check if 'time' exists in $temperature
if (isset($temperature['time'])) { $dateTime = new DateTime($temperature['time']);
} else {
// Handle case where 'time' is not set
$dateTime = null; // or set a default value or log error
}
} else {
// Handle case where 'current' key is not set or not an array
$temperature = null;
$deg = null;
$dateTime = null;
}
// Get the hour from the DateTime object
$hour = (int) $dateTime->format('H');
// Determine the time period based on the hour
if ($hour >= 5 && $hour < 12) {
$day = "/wp-content/uploads/2024/07/evening-icon.png";
$title = 'Morning';
} elseif ($hour >= 12 && $hour < 18) {
$day = "/wp-content/uploads/2024/07/afternoon-icon.png";
$title = 'Afternoon';
} elseif ($hour >= 18 && $hour < 22) {
$day = "/wp-content/uploads/2024/07/morning-icon.png";
$title = 'Evening';
} else {
$day = "/wp-content/uploads/2024/07/night-icon.png";
$title = 'Night';
}
return '<img alt="Paramount City Weather Status" src="'.$day.'" style="width:18px;margin-bottom: -4px;"> '.$deg.'° F';
?>