weather.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. trait WeatherHomepageItem
  3. {
  4. public function searchCityForCoordinates($query)
  5. {
  6. try {
  7. $query = $query ?? false;
  8. if (!$query) {
  9. $this->setAPIResponse('error', 'Query was not supplied', 422);
  10. return false;
  11. }
  12. $url = $this->qualifyURL('https://api.mapbox.com/geocoding/v5/mapbox.places/' . urlencode($query) . '.json?access_token=pk.eyJ1IjoiY2F1c2VmeCIsImEiOiJjazhyeGxqeXgwMWd2M2ZydWQ4YmdjdGlzIn0.R50iYuMewh1CnUZ7sFPdHA&limit=5&fuzzyMatch=true');
  13. $options = array('verify' => false);
  14. $response = Requests::get($url, array(), $options);
  15. if ($response->success) {
  16. $this->setAPIResponse('success', null, 200, json_decode($response->body));
  17. return json_decode($response->body);
  18. }
  19. } catch (Requests_Exception $e) {
  20. $this->setAPIResponse('error', $e->getMessage(), 500);
  21. return false;
  22. };
  23. }
  24. public function getWeatherAndAirData()
  25. {
  26. if (!$this->config['homepageWeatherAndAirEnabled']) {
  27. $this->setAPIResponse('error', 'Weather homepage item is not enabled', 409);
  28. return false;
  29. }
  30. if (!$this->qualifyRequest($this->config['homepageWeatherAndAirAuth'])) {
  31. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  32. return false;
  33. }
  34. if (empty($this->config['homepageWeatherAndAirLatitude']) && empty($this->config['homepageWeatherAndAirLongitude'])) {
  35. $this->setAPIResponse('error', 'Weather Latitude and/or Longitude were not defined', 422);
  36. return false;
  37. }
  38. $api['content'] = array(
  39. 'weather' => false,
  40. 'air' => false,
  41. 'pollen' => false
  42. );
  43. $apiURL = $this->qualifyURL('https://api.breezometer.com/');
  44. $info = '&lat=' . $this->config['homepageWeatherAndAirLatitude'] . '&lon=' . $this->config['homepageWeatherAndAirLongitude'] . '&units=' . $this->config['homepageWeatherAndAirUnits'] . '&key=b7401295888443538a7ebe04719c8394';
  45. try {
  46. $headers = array();
  47. $options = array();
  48. if ($this->config['homepageWeatherAndAirWeatherEnabled']) {
  49. $endpoint = '/weather/v1/forecast/hourly?hours=120&metadata=true';
  50. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  51. if ($response->success) {
  52. $apiData = json_decode($response->body, true);
  53. $api['content']['weather'] = ($apiData['error'] === null) ? $apiData : false;
  54. unset($apiData);
  55. }
  56. }
  57. if ($this->config['homepageWeatherAndAirAirQualityEnabled']) {
  58. $endpoint = '/air-quality/v2/current-conditions?features=breezometer_aqi,local_aqi,health_recommendations,sources_and_effects,dominant_pollutant_concentrations,pollutants_concentrations,pollutants_aqi_information&metadata=true';
  59. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  60. if ($response->success) {
  61. $apiData = json_decode($response->body, true);
  62. $api['content']['air'] = ($apiData['error'] === null) ? $apiData : false;
  63. unset($apiData);
  64. }
  65. }
  66. if ($this->config['homepageWeatherAndAirPollenEnabled']) {
  67. $endpoint = '/pollen/v2/forecast/daily?features=plants_information,types_information&days=1&metadata=true';
  68. $response = Requests::get($apiURL . $endpoint . $info, $headers, $options);
  69. if ($response->success) {
  70. $apiData = json_decode($response->body, true);
  71. $api['content']['pollen'] = ($apiData['error'] === null) ? $apiData : false;
  72. unset($apiData);
  73. }
  74. }
  75. } catch (Requests_Exception $e) {
  76. $this->writeLog('error', 'Weather And Air Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  77. $this->setAPIResponse('error', $e->getMessage(), 500);
  78. return false;
  79. };
  80. $api['content'] = isset($api['content']) ? $api['content'] : false;
  81. $this->setAPIResponse('success', null, 200, $api);
  82. return $api;
  83. }
  84. }