fork download
  1. <?php
  2.  
  3. $curl = curl_init();
  4.  
  5. CURLOPT_URL => 'https://a...content-available-to-author-only...o.com/v1/forecast?latitude=33.8991884&longitude=-118.1862416&current=temperature_2m&temperature_unit=fahrenheit',
  6. CURLOPT_RETURNTRANSFER => true,
  7. CURLOPT_ENCODING => '',
  8. CURLOPT_MAXREDIRS => 10,
  9. CURLOPT_TIMEOUT => 0,
  10. CURLOPT_FOLLOWLOCATION => true,
  11. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  12. CURLOPT_CUSTOMREQUEST => 'GET',
  13. ));
  14.  
  15. $response = curl_exec($curl);
  16.  
  17. curl_close($curl);
  18. //var_dump($response);
  19.  
  20.  
  21.  
  22. // Decode JSON response
  23. $data = json_decode($response, true);
  24.  
  25. print_r($data);
  26.  
  27.  
  28. // Get temperature
  29. // Check if $data is an array and if 'current' exists in $data
  30. if (isset($data['current']) && is_array($data['current'])) {
  31. $temperature = $data['current'];
  32.  
  33. // Check if 'temperature_2m' exists in $temperature
  34. if (isset($temperature['temperature_2m'])) {
  35. $deg = $temperature['temperature_2m'];
  36. } else {
  37. // Handle case where 'temperature_2m' is not set
  38. $deg = null; // or set a default value
  39. }
  40.  
  41. // Check if 'time' exists in $temperature
  42. if (isset($temperature['time'])) {
  43. $dateTime = new DateTime($temperature['time']);
  44. } else {
  45. // Handle case where 'time' is not set
  46. $dateTime = null; // or set a default value or log error
  47. }
  48. } else {
  49. // Handle case where 'current' key is not set or not an array
  50. $temperature = null;
  51. $deg = null;
  52. $dateTime = null;
  53. }
  54.  
  55. // Get the hour from the DateTime object
  56. $hour = (int) $dateTime->format('H');
  57.  
  58. // Determine the time period based on the hour
  59. if ($hour >= 5 && $hour < 12) {
  60. $day = "/wp-content/uploads/2024/07/evening-icon.png";
  61. $title = 'Morning';
  62. } elseif ($hour >= 12 && $hour < 18) {
  63. $day = "/wp-content/uploads/2024/07/afternoon-icon.png";
  64. $title = 'Afternoon';
  65. } elseif ($hour >= 18 && $hour < 22) {
  66. $day = "/wp-content/uploads/2024/07/morning-icon.png";
  67. $title = 'Evening';
  68. } else {
  69. $day = "/wp-content/uploads/2024/07/night-icon.png";
  70. $title = 'Night';
  71. }
  72.  
  73. return '<img alt="Paramount City Weather Status" src="'.$day.'" style="width:18px;margin-bottom: -4px;"> '.$deg.'&deg; F';
  74. ?>
  75.  
Success #stdin #stdout 0.03s 26248KB
stdin
Standard input is empty
stdout
Standard output is empty