PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/ajax/weather.php

https://gitlab.com/barrel/wallboard
PHP | 148 lines | 126 code | 13 blank | 9 comment | 7 complexity | 21c1155a1677288bf33ff48bf23e9b17 MD5 | raw file
  1. <?php
  2. /**
  3. * Get the weather info
  4. * @uses http://api.openweathermap.org
  5. * @param void
  6. * @return array
  7. */
  8. function weather(){
  9. date_default_timezone_set('America/New_York');
  10. $icons = array(
  11. 'sky_is_clear'=> 'B',
  12. 'few_clouds'=> 'H',
  13. 'scattered_clouds'=> 'N',
  14. 'overcast_clouds'=> 'N',
  15. 'broken_clouds'=> 'Y',
  16. 'shower_rain'=> 'R',
  17. 'moderate_rain'=> 'R',
  18. 'rain'=> '8',
  19. 'thunderstorm'=> '6',
  20. 'snow'=> 'W',
  21. 'mist'=> 'Q',
  22. 'fog'=> 'M',
  23. 'heavy_intensity_rain'=>'8',
  24. 'light_snow'=>'U',
  25. 'light_rain'=>'R',
  26. 'light_rain_and_snow'=>'V',
  27. 'light_intensity_drizzle'=>'R',
  28. 'drizzle'=>'R',
  29. 'heavy_intensity_drizzle'=>'R',
  30. 'very_heavy_rain'=>'8',
  31. 'extreme_rain'=>'8',
  32. 'freezing_rain'=>'8',
  33. 'light_intensity_shower_rain'=>'R',
  34. 'heavy_intensity_shower_rain'=>'8',
  35. 'unknown'=>')'
  36. );
  37. $weather_replace = array( ' ' => '_' );
  38. $weather = $query = array();
  39. $weather_api = array(
  40. 'url' => 'http://api.openweathermap.org/data/2.5/%s?%s',
  41. 'base' => array(
  42. 'mode' => 'xml',
  43. 'units' => 'imperial',
  44. 'APPID' => '4039c138d3a75fe829894408af96b78b'
  45. ), // base query data
  46. 'q' => array(
  47. array(
  48. 'id' => '5128581',
  49. ),
  50. array(
  51. 'q' => 'manhattan,ny',
  52. 'cnt' => '3'
  53. )
  54. ), // query data
  55. );
  56. foreach($weather_api['q'] as $idx => $q )
  57. $query[] = http_build_query( $weather_api['base'] + $q );
  58. $mapping = array(
  59. 'weather' => 0,
  60. 'forecast' => 0,
  61. 'forecast/daily' => 1
  62. );
  63. foreach($mapping as $str => $idx) {
  64. $url = sprintf($weather_api['url'], $str, $query[$idx]);
  65. $weatherxml = @file_get_contents($url);
  66. if ( $weatherxml && $weatherDataLater = new SimpleXMLElement($weatherxml)) {
  67. switch($str) {
  68. /** get the weather info */
  69. case 'weather':
  70. $forecast = $weatherDataLater->temperature->attributes();
  71. $status = $weatherDataLater->weather->attributes();
  72. $icon_index = strtolower(strtr($status->value, $weather_replace));
  73. if ( empty($icons[$icon_index])) {
  74. error_log("Missing Icon: $icon_index");
  75. $icon_index = 'unknown';
  76. }
  77. $weather['now_high'] = (int)$forecast->max;
  78. $weather['now_low'] = (int)$forecast->min;
  79. $weather['now_temperature'] = (int)$forecast->value;
  80. $weather['now_icon'] = sprintf(
  81. '<span class="weather-icon">%s</span>', $icons[$icon_index]
  82. );
  83. break;
  84. /** get the forecast info */
  85. case 'forecast':
  86. $forecast = $weatherDataLater->forecast;
  87. foreach($forecast->{'time'} as $time){
  88. $symbol = $time->symbol->attributes();
  89. $icon_index = strtolower(strtr($symbol->name, $weather_replace));
  90. if ( empty($icons[$icon_index])) {
  91. error_log("Missing Icon: $icon_index");
  92. $icon_index = 'unknown';
  93. }
  94. $temperature = $time->temperature->attributes();
  95. $weather['next_hour_icon'] = sprintf(
  96. '<span class="weather-icon">%s</span>', $icons[$icon_index]
  97. );
  98. $weather['next_hour_temperature'] = (int)$temperature->value;
  99. $first = false;
  100. if(!$first) break;
  101. }
  102. break;
  103. /** get the daily forecast info */
  104. default:
  105. $forecast = $weatherDataLater->forecast;
  106. $i=0;
  107. foreach($forecast->{'time'} as $time){
  108. $symbol = $time->symbol->attributes();
  109. $temperature = $time->temperature->attributes();
  110. $icon_index = strtolower(strtr($symbol->name, $weather_replace));
  111. if ( empty($icons[$icon_index])) {
  112. error_log("Missing Icon: $icon_index");
  113. $icon_index = 'unknown';
  114. }
  115. if ($i==1){
  116. $weather['tomorrow_icon'] = sprintf(
  117. '<span class="weather-icon">%s</span>', $icons[$icon_index]
  118. );
  119. $weather['tomorrow_temperature'] = (int)$temperature->day;
  120. } elseif ($i==2){
  121. $weather['next_icon'] = sprintf(
  122. '<span class="weather-icon">%s</span>', $icons[$icon_index]
  123. );
  124. $weather['next_temperature'] = (int)$temperature->day;
  125. }
  126. $i++;
  127. }
  128. $weather['date'] = date("M. j @ g:ia");
  129. }
  130. }
  131. }
  132. return $weather;
  133. }
  134. echo json_encode( weather() );
  135. ?>