PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sh/weather_xml.php

https://github.com/michaelmwu/saratogahigh.com
PHP | 334 lines | 260 code | 28 blank | 46 comment | 9 complexity | 45b38229516a7ed33158e8ddf27f6b1c MD5 | raw file
  1. <?
  2. /*
  3. +++++++++++++++++++++++++++++++
  4. +
  5. + Weather_xml.php written by
  6. + admin@notonebit.com
  7. +
  8. + Last changed: December 21, 2004
  9. + Version 1.3
  10. +
  11. +++++++++++++++++++++++++++++++
  12. */
  13. // Sign up for Weather.com's free XML service at http://www.weather.com/services/xmloap.html
  14. // Setup database connection
  15. $db_host = "";
  16. $db_user = "";
  17. $db_pwd = "";
  18. $db_name = "";
  19. $connection = mysql_connect($db_host, $db_user, $db_pwd) or die("Could not connect");
  20. mysql_select_db($db_name) or die("Could not select database");
  21. // Set Local variables
  22. $partner_ID = "";
  23. $license_key = "";
  24. $location = urlencode($_REQUEST['loc']); //search on location (city or zip)
  25. $loc_id = $_REQUEST['id']; // specific town id
  26. $length = $_REQUEST['length']; // Forecast length
  27. $image_size = "64x64"; // 32x32, 64x64, or 128x128 - size of daily weather images
  28. if (!($length >= 1 And $length <= 10)) $length = 10;
  29. // First URL for searching, second for detail.
  30. $search_url = "http://xoap.weather.com/search/search?where=$location";
  31. $forecast_url = "http://xoap.weather.com/weather/local/$loc_id?cc=*&dayf=$length&prod=xoap&par=$partner_ID&key=$license_key";
  32. /*
  33. cc Current Conditions OPTIONAL VALUE IGNORED
  34. dayf Multi-day forecast information for some or all forecast elements OPTIONAL VALUE = [ 1..10 ]
  35. link Links for weather.com pages OPTIONAL VALUE = xoap
  36. par Application developers Id assigned to you REQUIRED VALUE = {partner id}
  37. prod The XML Server product code REQUIRED VALUE = xoap
  38. key The license key assigned to you REQUIRED VALUE = {license key}
  39. unit Set of units. Standard or Metric OPTIONAL VALUES = [ s | m ] DEFAULT = s
  40. */
  41. function html_header() // Write html header to screen
  42. {
  43. $header = <<< end_of_output
  44. <html>
  45. <head>
  46. <title></title>
  47. <style>
  48. <!--
  49. body, p { font-size: 10pt; font-family: Verdana }
  50. td { font-size: 10pt; font-family: Verdana }
  51. -->
  52. </style>
  53. </head>
  54. <body>
  55. end_of_output;
  56. echo $header;
  57. }
  58. if ($location) // Determine URL to use. If location is passed, we're searching for a city or zip. Elese we're retrieving a forecast.
  59. {
  60. $url = $search_url;
  61. }
  62. else
  63. {
  64. $url = $forecast_url;
  65. }
  66. if ($location Or $loc_id) // If city, zip, or weather.com city id passed, do XML query. $loc_id is a weather.com city code, $location is user entered city or zip
  67. {
  68. /*
  69. query db for md5 of url
  70. if doesn't exist, insert into db
  71. if exists, check date, if under X hours use db content
  72. if older then X hours, pull from weather.com and update db
  73. to delete old data: when querying, delete all records older than X hours
  74. */
  75. $datetime = date("Y-m-d h:i:s");
  76. $xml_url = md5($url);
  77. $interval = 12; // Hours to keep data in db before being considered old
  78. $expires = $interval*60*60;
  79. $expiredatetime = date("Y-m-d H:i:s", time() - $expires);
  80. // Delete expired records
  81. $query = "DELETE FROM weather_xml WHERE last_updated < '$expiredatetime'";
  82. $result = mysql_query($query) or die('Invalid query: ' . mysql_error());
  83. $query = "SELECT * FROM weather_xml WHERE xml_url = '$xml_url'";
  84. $result = mysql_query($query) or die('Invalid query: ' . mysql_error());
  85. $row = mysql_fetch_array($result);
  86. $time_diff = strtotime($datetime) - strtotime($row['last_updated']);
  87. if (mysql_num_rows($result) < 1) // Data not in table - Add.
  88. {
  89. // Get XML Query Results from Weather.com
  90. $fp = fopen($url,"r");
  91. while (!feof ($fp))
  92. $xml .= fgets($fp, 4096);
  93. fclose ($fp);
  94. // Fire up the built-in XML parser
  95. $parser = xml_parser_create( );
  96. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  97. // Set tag names and values
  98. xml_parse_into_struct($parser,$xml,$values,$index);
  99. // Close down XML parser
  100. xml_parser_free($parser);
  101. $xml = str_replace("'","",$xml); // Added to handle cities with apostrophies in the name like T'Bilisi, Georgia
  102. if ($loc_id) // Only inserts forecast feed, not search results feed, into db
  103. {
  104. $query = "INSERT INTO weather_xml VALUES ('$xml_url', '$xml', '$datetime')";
  105. $result = mysql_query($query) or die('Invalid query: ' . mysql_error());
  106. }
  107. }
  108. else // Data in table, and it is within expiration period - do not load from weather.com and use cached copy instead.
  109. {
  110. $xml = $row['xml_data'];
  111. // Fire up the built-in XML parser
  112. $parser = xml_parser_create( );
  113. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  114. // Set tag names and values
  115. xml_parse_into_struct($parser,$xml,$values,$index);
  116. // Close down XML parser
  117. xml_parser_free($parser);
  118. }
  119. // Debugging output
  120. //echo "<pre>";
  121. //print_r($xml);
  122. //print_r($index);
  123. //print_r($values);
  124. //echo "</pre>";
  125. }
  126. if ($loc_id) // Location code selected - Display detail info. A specific city has been selected from the drop down menu. Get forecast.
  127. {
  128. $city = htmlspecialchars($values[$index[dnam][0]][value]);
  129. $unit_temp = $values[$index[ut][0]][value];
  130. $unit_speed = $values[$index[us][0]][value];
  131. $unit_precip = $values[$index[up][0]][value];
  132. $unit_pressure = $values[$index[ur][0]][value];
  133. $sunrise = $values[$index[sunr][0]][value];
  134. $sunset = $values[$index[suns][0]][value];
  135. $timezone = $values[$index[tzone][0]][value];
  136. $last_update = $values[$index[lsup][0]][value];
  137. $curr_temp = $values[$index[tmp][0]][value];
  138. $curr_flik = $values[$index[flik][0]][value];
  139. $curr_text = $values[$index[t][0]][value];
  140. $curr_icon = $values[$index[icon][0]][value];
  141. $counter = 0;
  142. $row_counter = 2;
  143. html_header(); // Output header
  144. echo "<b><font size=4>Weather report for $city</font></b><br /><font size=1>(Last updated $last_update).</font><br />\n";
  145. echo "<p><table><tr><td><img border=\"1\" src=\"/images/weather/128x128/$curr_icon.png\" alt=\"$curr_text\"></td><td>";
  146. echo "<font size=3>Currently: <b>$curr_temp&#730; $unit_temp</b></font><br />\n";
  147. echo "Feels Like: $curr_flik&#730; $unit_temp<br />Current conditions: $curr_text<br />\n";
  148. echo "Sunrise: $sunrise.<br />Sunset: $sunset.<br />\n";
  149. echo "</td></tr></table></p>";
  150. $color_bar = <<< end_of_output
  151. <table border="1" cellspacing="0" cellpadding="4" style="border-collapse: collapse" bordercolor="#000000">
  152. <tr>
  153. <td width="9%" bgcolor="#CC99CC" align="center">
  154. <font face="Verdana" size="1">&lt;10</font></td>
  155. <td width="9%" bgcolor="#9966FF" align="center">
  156. <font face="Verdana" size="1">10's</font></td>
  157. <td width="9%" bgcolor="#3399FF" align="center">
  158. <font face="Verdana" size="1">20's</font></td>
  159. <td width="9%" bgcolor="#99CCFF" align="center">
  160. <font face="Verdana" size="1">30's</font></td>
  161. <td width="9%" bgcolor="#66CC66" align="center">
  162. <font face="Verdana" size="1">40's</font></td>
  163. <td width="9%" bgcolor="#FFFF99" align="center">
  164. <font face="Verdana" size="1">50's</font></td>
  165. <td width="9%" bgcolor="#FFCC33" align="center">
  166. <font face="Verdana" size="1">60's</font></td>
  167. <td width="9%" bgcolor="#FF9933" align="center">
  168. <font face="Verdana" size="1">70's</font></td>
  169. <td width="9%" bgcolor="#FF6600" align="center">
  170. <font face="Verdana" size="1">80's</font></td>
  171. <td width="9%" bgcolor="#FF0000" align="center">
  172. <font face="Verdana" size="1">90's</font></td>
  173. <td width="10%" bgcolor="#990000" align="center">
  174. <font face="Verdana" size="1" color="#FFFFFF">100+</font></td>
  175. </tr>
  176. </table>
  177. end_of_output;
  178. echo $color_bar;
  179. echo "<table border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#C0C0C0\"><tr><th>Date</th><th>High/Low</th><th>Day</th><th>Night</th></tr>";
  180. foreach ($index[day] as $day)
  181. {
  182. if ($values[$day][attributes][t] != "")
  183. {
  184. // ($row_counter%2==0) ? $row_color = "#CCE6FF": $row_color = "#CCCDFF";
  185. $row_color = "#EEEECC";
  186. $img_day = ($counter + 1) * 2;
  187. $img_night = (($counter + 1) * 2) + 1;
  188. $day_text = (($counter + 1) * 3) + $counter + 2;
  189. $day_wind = ((($counter + 1) * 3) + $counter) + 1;
  190. $day_windspeed = (($counter + 1) * 2) - 1;
  191. $day_windgust = (($counter + 1) * 2) - 1;
  192. $day_humidity = (($counter + 1) * 2) - 1;
  193. $day_precip = $counter * 2;
  194. $night_text = ((($counter + 1) * 3) + $counter) + 4;
  195. $night_wind = ((($counter + 1) * 3) + $counter) + 3;
  196. $night_windspeed = ($counter + 1) * 2;
  197. $night_windgust = ($counter + 1) * 2;
  198. $night_humidity = ($counter + 1) * 2;
  199. $night_precip = ($counter * 2) + 1;
  200. if ($values[$index[hi][$counter]][value] >= 0) $heat_color = "#CC99CC";
  201. if ($values[$index[hi][$counter]][value] >= 10) $heat_color = "#9966FF";
  202. if ($values[$index[hi][$counter]][value] >= 20) $heat_color = "#3399FF";
  203. if ($values[$index[hi][$counter]][value] >= 30) $heat_color = "#99CCFF";
  204. if ($values[$index[hi][$counter]][value] >= 40) $heat_color = "#66CC66";
  205. if ($values[$index[hi][$counter]][value] >= 50) $heat_color = "#FFFF99";
  206. if ($values[$index[hi][$counter]][value] >= 60) $heat_color = "#FFCC33";
  207. if ($values[$index[hi][$counter]][value] >= 70) $heat_color = "#FF9933";
  208. if ($values[$index[hi][$counter]][value] >= 80) $heat_color = "#FF6600";
  209. if ($values[$index[hi][$counter]][value] >= 90) $heat_color = "#FF0000";
  210. if ($values[$index[hi][$counter]][value] >= 100) $heat_color = "#990000";
  211. if ($values[$index[hi][$counter]][value] == "N/A") $heat_color = "#EEEECC";
  212. echo "<tr><td bgcolor=\"$heat_color\"><b>" . $values[$day][attributes][t] . ", " . $values[$day][attributes][dt] . "</b></td>";
  213. echo "<td bgcolor=\"$row_color\"><b>Hi: " . $values[$index[hi][$counter]][value] . "&#730; $unit_temp\n";
  214. echo "<hr noshade height=\"1\">Lo: " . $values[$index[low][$counter]][value] . "&#730; $unit_temp</b></td>\n";
  215. echo "<td bgcolor=\"$row_color\" nowrap><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"><tr><td><b>\n";
  216. echo $values[$index[t][$day_wind]][value] . "</b><br /> ";
  217. echo "<font size=1>Sunrise: " . $values[$index[sunr][$counter]][value] . "<br />";
  218. echo "Wind: " . $values[$index[t][$day_text]][value] . " " . $values[$index[s][$day_windspeed]][value] . " $unit_speed";
  219. echo "<br />Humidity: " . $values[$index[hmid][$day_humidity]][value] . "%<br />Precip: " . $values[$index[ppcp][$day_precip]][value] . "%</font></td>";
  220. echo "<td bgcolor=\"$row_color\" align=\"right\"><img border=\"1\" src=\"/images/weather/$image_size/" . $values[$index[icon][$img_day]][value] . ".png\" alt=\"" . $values[$index[t][$day_text]][value] . "\"></td></tr></table></td>";
  221. echo "<td bgcolor=\"$row_color\" nowrap><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"><tr><td><b>\n";
  222. echo $values[$index[t][$night_wind]][value] . "</b><br />";
  223. echo "<font size=1>Sunset: " . $values[$index[suns][$counter]][value] . "<br />";
  224. echo "Wind: " . $values[$index[t][$night_text]][value] . " " . $values[$index[s][$night_windspeed]][value] . " $unit_speed";
  225. echo "<br />Humidity: " . $values[$index[hmid][$night_humidity]][value] . "%<br />Precip: " . $values[$index[ppcp][$night_precip]][value] . "%</font></td>\n";
  226. echo "<td bgcolor=\"$row_color\" align=\"right\"><img border=\"1\" src=\"/images/weather/$image_size/" . $values[$index[icon][$img_night]][value] .".png\" alt=\"" . $values[$index[t][$night_text]][value] . "\"></tr></table></td>";
  227. echo "</tr>";
  228. $counter++;
  229. $row_counter++;
  230. }
  231. }
  232. echo "</table>";
  233. }
  234. if ($location And is_array($index[loc])) // A city name has been entered and data returned from weather.com, draw drop down menu of matches
  235. {
  236. if (count($index[loc]) == 1) // If just one match returned, send to detail screen - no need to draw option box for one option.
  237. {
  238. $location_code = $values[$index[loc][0]][attributes][id];
  239. header("Location: weather_xml.php?id=$location_code&length=10"); // Nees html_header because of this redirect.
  240. exit();
  241. }
  242. html_header();
  243. echo "<form action=\"weather_xml.php\" method=\"POST\">";
  244. echo "Select a city: <select size=\"1\" name=\"id\">\n";
  245. // Loop through the XML, setting values
  246. foreach ($index[loc] as $key=>$val)
  247. {
  248. echo "<option value=\"";
  249. echo $values[$val][attributes][id]; // City code
  250. echo "\">";
  251. echo $values[$val][value]; // City name
  252. echo "</option>\n";
  253. }
  254. echo '</select>';
  255. ?>
  256. Length of forecast
  257. <select size="1" name="length">
  258. <option selected>10</option>
  259. <option>9</option>
  260. <option>8</option>
  261. <option>7</option>
  262. <option>6</option>
  263. <option>5</option>
  264. <option>4</option>
  265. <option>3</option>
  266. <option>2</option>
  267. <option>1</option>
  268. </select>
  269. <input type="submit" value="Get Weather">
  270. </form>
  271. <?
  272. }
  273. elseif ($location) // City or zip entered but no match returned from weather.com
  274. {
  275. html_header();
  276. echo "No city found. Please enter another city or zip code.<br />\n";
  277. }
  278. if (empty($location) And empty($loc_id))
  279. {
  280. html_header();
  281. }
  282. ?>
  283. <form action="weather_xml.php" method="POST">
  284. <p>Enter a city or zip code: <input type="text" name="loc" size="20"><input type="submit" value="Search"></p>
  285. </form>
  286. Weather data provided by <a href="http://www.weather.com/?prod=xoap&par=<?=$partner_ID?>">weather.com <img src="/images/weather/logos/TWClogo_32px.png" border="0" alt="The Weather Channel"></a>
  287. </body>
  288. </html>