Simple private API to get current weather information at a lon/lat position from Environment Canada (EC).
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

78 行
2.0KB

  1. <?php
  2. require '../../vendor/autoload.php';
  3. require '../class/SitesFactory.php';
  4. require '../../config.php';
  5. require '../helpers.php';
  6. $dbh = db($config['dbUser'], $config['dbPassword'], $config['dbName']);
  7. // Fetch sites from endpoint
  8. $sites = new SitesFactory();
  9. $sites->_fillSites();
  10. // Extract site codes
  11. $siteCodes = $sites->getCodeList();
  12. $nbSiteCodes = sizeof($siteCodes);
  13. // Build prepared statement
  14. $query = find_new_sites_query($nbSiteCodes);
  15. $statement = $dbh->prepare($query);
  16. // Fill the parameters
  17. for ($i = 0; $i < $nbSiteCodes; $i += 1) {
  18. $statement->bindValue($i + 1, $siteCodes[$i], PDO::PARAM_STR);
  19. }
  20. // Get the codes of all unknown sites
  21. $statement->execute();
  22. $new_sites_codes = $statement->fetchAll(PDO::FETCH_COLUMN, 0);
  23. $filledSites = [];
  24. // Fill the coordinates for new sites
  25. foreach($new_sites_codes as $new_site_code) {
  26. $new_site = $sites->getSiteByCode($new_site_code);
  27. try {
  28. $new_site->fillCoords();
  29. $filledSites[] = $new_site;
  30. } catch(Exception $e) {
  31. error_log('Unable to get coords for site: ' . $new_site->getName());
  32. echo $e->getMessage();
  33. }
  34. // Don't hammer the endpoint
  35. // (each fillCoords() is a HTTP request)
  36. sleep(2);
  37. }
  38. $nbNewSites = sizeof($filledSites);
  39. // No new sites, exit normally
  40. if ($nbNewSites === 0) {
  41. echo "No new sites\n";
  42. exit(0);
  43. }
  44. // Build prepared statement
  45. $query = insert_sites_query($nbNewSites);
  46. $statement = $dbh->prepare($query);
  47. // Fill the parameters
  48. $i = 0;
  49. foreach($filledSites as $filledSite) {
  50. $strIdx = (string)$i;
  51. $statement->bindValue(":code$strIdx", $filledSite->getCode(), PDO::PARAM_STR);
  52. $statement->bindValue(":name$strIdx", $filledSite->getName(), PDO::PARAM_STR);
  53. $statement->bindValue(":province$strIdx", $filledSite->getProvince(), PDO::PARAM_STR);
  54. $statement->bindValue(":lon$strIdx", $filledSite->getCoords()->lon, PDO::PARAM_INT);
  55. $statement->bindValue(":lat$strIdx", $filledSite->getCoords()->lat, PDO::PARAM_INT);
  56. $i += 1;
  57. }
  58. // Insert new sites in DB
  59. $statement->execute();