/modules/weather.module.php

https://github.com/achernya/simmons-dash · PHP · 168 lines · 156 code · 8 blank · 4 comment · 30 complexity · 8730d20395e0253a9cd2856833c2ceae MD5 · raw file

  1. <?php
  2. /* DATA */
  3. /* $zipcode ='50613'; */
  4. $zipcode = $_GET['zipcode'];
  5. /* DISPLAY */
  6. // found at php.net
  7. function xml2array($url, $get_attributes = 1, $priority = 'tag') {
  8. $contents = "";
  9. if (!function_exists('xml_parser_create'))
  10. {
  11. return array ();
  12. }
  13. $parser = xml_parser_create('');
  14. if (!($fp = @ fopen($url, 'rb')))
  15. {
  16. return array ();
  17. }
  18. while (!feof($fp))
  19. {
  20. $contents .= fread($fp, 8192);
  21. }
  22. fclose($fp);
  23. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
  24. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  25. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  26. xml_parse_into_struct($parser, trim($contents), $xml_values);
  27. xml_parser_free($parser);
  28. if (!$xml_values)
  29. return; //Hmm...
  30. $xml_array = array ();
  31. $parents = array ();
  32. $opened_tags = array ();
  33. $arr = array ();
  34. $current = & $xml_array;
  35. $repeated_tag_index = array ();
  36. foreach ($xml_values as $data)
  37. {
  38. unset ($attributes, $value);
  39. extract($data);
  40. $result = array ();
  41. $attributes_data = array ();
  42. if (isset ($value))
  43. {
  44. if ($priority == 'tag')
  45. $result = $value;
  46. else
  47. $result['value'] = $value;
  48. }
  49. if (isset ($attributes) and $get_attributes)
  50. {
  51. foreach ($attributes as $attr => $val)
  52. {
  53. if ($priority == 'tag')
  54. $attributes_data[$attr] = $val;
  55. else
  56. $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  57. }
  58. }
  59. if ($type == "open")
  60. {
  61. $parent[$level -1] = & $current;
  62. if (!is_array($current) or (!in_array($tag, array_keys($current))))
  63. {
  64. $current[$tag] = $result;
  65. if ($attributes_data)
  66. $current[$tag . '_attr'] = $attributes_data;
  67. $repeated_tag_index[$tag . '_' . $level] = 1;
  68. $current = & $current[$tag];
  69. }
  70. else
  71. {
  72. if (isset ($current[$tag][0]))
  73. {
  74. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  75. $repeated_tag_index[$tag . '_' . $level]++;
  76. }
  77. else
  78. {
  79. $current[$tag] = array (
  80. $current[$tag],
  81. $result
  82. );
  83. $repeated_tag_index[$tag . '_' . $level] = 2;
  84. if (isset ($current[$tag . '_attr']))
  85. {
  86. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  87. unset ($current[$tag . '_attr']);
  88. }
  89. }
  90. $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  91. $current = & $current[$tag][$last_item_index];
  92. }
  93. }
  94. elseif ($type == "complete")
  95. {
  96. if (!isset ($current[$tag]))
  97. {
  98. $current[$tag] = $result;
  99. $repeated_tag_index[$tag . '_' . $level] = 1;
  100. if ($priority == 'tag' and $attributes_data)
  101. $current[$tag . '_attr'] = $attributes_data;
  102. }
  103. else
  104. {
  105. if (isset ($current[$tag][0]) and is_array($current[$tag]))
  106. {
  107. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  108. if ($priority == 'tag' and $get_attributes and $attributes_data)
  109. {
  110. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  111. }
  112. $repeated_tag_index[$tag . '_' . $level]++;
  113. }
  114. else
  115. {
  116. $current[$tag] = array (
  117. $current[$tag],
  118. $result
  119. );
  120. $repeated_tag_index[$tag . '_' . $level] = 1;
  121. if ($priority == 'tag' and $get_attributes)
  122. {
  123. if (isset ($current[$tag . '_attr']))
  124. {
  125. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  126. unset ($current[$tag . '_attr']);
  127. }
  128. if ($attributes_data)
  129. {
  130. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  131. }
  132. }
  133. $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
  134. }
  135. }
  136. }
  137. elseif ($type == 'close')
  138. {
  139. $current = & $parent[$level -1];
  140. }
  141. }
  142. return ($xml_array);
  143. }
  144. while(true) {
  145. $url = 'http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=';
  146. $data = xml2array($url . $zipcode);
  147. if (empty($data)) {
  148. sleep(5);
  149. } else {
  150. break;
  151. }
  152. }
  153. $data = $data['current_observation'];
  154. ?>
  155. <div class="vertical-center height-3">
  156. <span class='jumbo'>Weather in <?php echo $data['display_location']['city'] ?></span>
  157. <div>
  158. <?php echo $data['temp_f'] . '&deg; F, ' . $data['weather'] ?></li>
  159. </div>
  160. </div>