gps_ajax.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. ini_set("display_errors", "0");
  3. //error_reporting(E_ALL);
  4. date_default_timezone_set('UTC');
  5. require("connection.php");
  6. // Opens a connection to a MySQL server
  7. $db = new PDO("mysql:host=$dbhost;dbname=$db;charset=utf8mb4", $dbuser, $dbpass);
  8. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  10. $cache_file = "data.txt";
  11. $query = "SHOW TABLE STATUS FROM arduino LIKE 'markers'";
  12. $stmt = $db->query($query);
  13. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  14. if (file_exists($cache_file) && strtotime($row['Update_time']) <= filemtime ($cache_file)) {
  15. header("Location: $cache_file?cache=" . filemtime ($cache_file));
  16. die();
  17. } else {
  18. // Select all the rows in the markers table
  19. $query = "SELECT * FROM markers ORDER BY timestamp";
  20. $stmt = $db->query($query);
  21. // Iterate through the rows, adding XML nodes for each
  22. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  23. $row['lat'] = doubleval($row['lat']);
  24. $row['lng'] = doubleval($row['lng']);
  25. $row['alt'] = floatval($row['alt']);
  26. $row['hdop'] = intval($row['hdop']);
  27. $row['timestamp'] = strtotime($row['timestamp']);
  28. //$row['format'] = date('d.m.Y H:i:s', $row['timestamp']);
  29. $json[] = $row;
  30. }
  31. $txt=json_encode($json);
  32. file_put_contents($cache_file, $txt, LOCK_EX);
  33. echo $txt;
  34. }
  35. ?>