Example #1
0
 function get($rpt)
 {
     // URL for the XML file
     $xmlurl = "http://www.weather.gov/data/current_obs/{$rpt}.xml";
     // Base url for the icons
     $imgpath = "http://weather.gov/weather/images/fcicons";
     self::$itemdata = "";
     self::$itemname = "";
     self::$wxdata = array();
     $icons = self::defineIcons();
     $icon = "";
     $data = "";
     $report = "";
     // create a new CURL resource
     if ($ch = curl_init()) {
         // set URL and other appropriate options
         curl_setopt($ch, CURLOPT_URL, $xmlurl);
         curl_setopt($ch, CURLOPT_HEADER, trus);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         // grab URL and pass it to the browser
         $data = curl_exec($ch);
         $r = curl_getinfo($ch);
         //,CURLINFO_HTTP_CODE);
         // close CURL resource, and free up system resources
         curl_close($ch);
         // Create an XML parser
         $xml_parser = xml_parser_create();
         // Use case-folding so we are sure to find the tag in $map_array
         // This will force all tags to upper case so we don't have to worry
         // about matching the case of the original in our tests.
         xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
         // Assign the element starting and ending event handlers
         xml_set_element_handler($xml_parser, array(self, "startElement"), array(self, "endElement"));
         // Assign a function to handle character data
         xml_set_character_data_handler($xml_parser, array(self, "characterData"));
         // Parse the file. This will place the data into an associative
         // array assigned to the self::$wxdata variable
         xml_parse($xml_parser, $data, true);
         // Free the parser object
         xml_parser_free($xml_parser);
         // The OBSERVATION_TIME field of the returned XML will be in the
         // format "Last Updated on May 18, 8:53 am CDT"
         // We're going to change the format a bit.
         // Strip out the "Last Updated on " portion of the date/time
         // so we can display that separately in our tabular output
         $datetime = str_replace("Last Updated on ", "", self::$wxdata['OBSERVATION_TIME']);
         // We now have the format as "May 18, 8:53 am CDT"
         // Now, get the time zone. It will be everything from
         // the last space character to the end of the string.
         $z = strrchr($datetime, " ");
         // Get the current year
         $year = date("Y");
         // Now, we stuff the year into the string, following the comma.
         $datetime = str_replace(",", ", {$year}", $datetime);
         // This does leave a small potential issue where, if you get a
         // report between midnight and 1 a.m. on January 1, or the server
         // is in a significantly different time zone than the report it
         // could be as late as 4 a.m. the year will be wrong because the
         // report will be from the previous year. I suppose it would be
         // possible to correct for that, but for that little bit, I'm
         // not going to worry about it.
         // Now, strip out the time zone
         $datetime = str_replace($z, "", $datetime);
         // Format the date and time the way we want it and add
         // back the time zone
         $datetime = date("l F j, Y g:i A", strtotime($datetime)) . $z;
         self::$wxdata['OBSERVATION_TIME'] = $datetime;
         // Get the WEATHER element
         $wx = trim(self::$wxdata['WEATHER']);
         // Now, get the icon to match the weather
         foreach ($icons as $k => $i) {
             $a = explode(" | ", $i);
             if (is_numeric(array_search($wx, $a))) {
                 self::$wxdata['ICON'] = "{$imgpath}/{$k}.jpg";
                 break;
             }
         }
         // Replace any null elements with "Not available"
         foreach (array_keys(self::$wxdata) as $key) {
             self::$wxdata[$key] = self::$wxdata[$key] == "NULL" ? "Not available" : self::$wxdata[$key];
         }
         // If we got humidity
         if (is_numeric(self::$wxdata['RELATIVE_HUMIDITY'])) {
             // Append a percent sign
             self::$wxdata['RELATIVE_HUMIDITY'] .= "%";
         }
         // Do some formatting to make the output a little friendlier
         if (self::$wxdata['VISIBILITY_MI'] == "NA") {
             self::$wxdata['VISIBILITY'] = "Not available";
         }
         if (self::$wxdata['VISIBILITY'] != "Not available") {
             self::$wxdata['VISIBILITY'] = 1 * self::$wxdata['VISIBILITY_MI'] . " miles";
         }
         // If we got wind data
         if (is_numeric(self::$wxdata['WIND_MPH'])) {
             // We're going to output wind data as both MPH from a cardinal direction
             // and as Knots from a direction in degrees
             // Calculate the value for Knots
             self::$wxdata['WIND_KNOTS'] = self::$wxdata['WIND_MPH'] / 1.15;
             // Format the output
             $wind = sprintf("From the %s at %d mph (%03.0f° at %d knots)", self::$wxdata['WIND_DIR'], self::$wxdata['WIND_MPH'], self::$wxdata['WIND_DEGREES'], self::$wxdata['WIND_KNOTS']);
             // If we got a value for wind gusts
             if (is_numeric(self::$wxdata['WIND_GUST_MPH']) && self::$wxdata['WIND_GUST_MPH'] > 0) {
                 // add it into the wind string
                 $wind = str_replace("mph", "gusting to " . self::$wxdata['WIND_GUST_MPH'] . " mph<br>", $wind);
                 $knots = sprintf("%d", self::$wxdata['WIND_GUST_MPH'] / 1.15);
                 $wind = str_replace("knots", "gusting to {$knots} knots", $wind);
             }
         } else {
             // Otherwise, if wind is zero, we'll show "Calm"
             $wind = self::$wxdata['WIND_MPH'] == "Not available" ? "Not available" : "Calm";
         }
         // Done with wind
         self::$wxdata['WIND_STRING'] = $wind;
     }
     // Done getting and formatting the data
     return self::$wxdata;
 }