weather_inc.php 1.1 KB

1234567891011121314151617181920212223242526
  1. <?php
  2. /**
  3. * Handles all the hard weather db stuff, just provide $VARS lat and long.
  4. *
  5. * Get the contents of the current weather with $currently
  6. */
  7. require_once 'latlong_validate.php';
  8. // Round to 2 digits (approx. 1.1km)
  9. $lat = number_format((float) $VARS['lat'], 2, '.', '');
  10. $long = number_format((float) $VARS['long'], 2, '.', '');
  11. // Delete old records
  12. $database->delete('weathercache', ["date[<]" => date('Y-m-d H:i:s', strtotime('-30 minutes'))]);
  13. // If we don't get a cache hit, request from the API
  14. if (!$database->has('weathercache', ["AND" => ["latitude" => $lat, "longitude" => $long]])) {
  15. $weather = json_decode(file_get_contents("https://api.darksky.net/forecast/" . DARKSKY_APIKEY . "/$lat,$long"), TRUE);
  16. $currentjson = json_encode($weather['currently']);
  17. $database->insert('weathercache', ["latitude" => $lat, "longitude" => $long, "#date" => "NOW()", "currentjson" => $currentjson]);
  18. }
  19. // Get the cached record and put it in a variable
  20. $currently = json_decode($database->select('weathercache', 'currentjson', ["AND" => ["latitude" => $lat, "longitude" => $long]])[0], TRUE);