CVS: phpweather data_retrieval.php,1.23,1.24
Brought to you by:
iridium
From: Martin G. <gim...@us...> - 2002-08-27 23:26:29
|
Update of /cvsroot/phpweather/phpweather In directory usw-pr-cvs1:/tmp/cvs-serv32029 Modified Files: data_retrieval.php Log Message: Patch from Reini Urban <ru...@x-...>. In PHP 4.2 the default for allow_url_fopen is off - so we now download the METAR reports ourselves. Index: data_retrieval.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/data_retrieval.php,v retrieving revision 1.23 retrieving revision 1.24 diff -u -3 -r1.23 -r1.24 --- data_retrieval.php 15 May 2002 22:23:26 -0000 1.23 +++ data_retrieval.php 27 Aug 2002 23:26:25 -0000 1.24 @@ -53,7 +53,7 @@ * Returns the current ICAO. * * @access public - * @return string The station-ICAO + * @return string The ICAO of the current station. */ function get_icao() { return $this->properties['icao']; @@ -62,9 +62,9 @@ /** * Sets the station or rather the ICAO. * - * It also clears the METAR and decodes METAR if the station is - * different from the old one. If the new station is the same as - * the old one, nothing is changed. + * It also clears the METAR and the decoded METAR data if the ICAO + * is different from the old one. If the new ICAO is the same as the + * old one, nothing is changed. * * @access public * @param string The ICAO of the new station. @@ -87,8 +87,8 @@ /** * Retrieves a raw METAR, either from the web or from a database. * - * If the METAR is already set, then it just returns that. - * If it's not set, then it tries to get it from the database. + * If the METAR is already set, then it just returns that. If it's + * not set, then it tries to get it from the database. * * @access public * @return string The raw METAR. @@ -198,53 +198,57 @@ * @access public * @return string The raw METAR. */ + function get_metar_from_web($new_station) { $metar = ''; $icao = $this->get_icao(); - + $host = 'weather.noaa.gov'; + $location = "/pub/data/observations/metar/stations/$icao.TXT"; + $request = "HTTP/1.1\r\n" . + "If-Modified-Since: Sat, 29 Oct 1994 09:00:00 GMT\r\n" . + "Pragma: no-cache\r\n". + "Cache-Contol: no-cache\r\n"; if ($this->properties['use_proxy']) { /* We use a proxy */ /* Inspirated by code from Paul Kairis <Pau...@sa...> */ - $fp = fsockopen($this->properties['proxy_host'], $this->properties['proxy_port']); - if ($fp) { - fputs($fp, 'GET http://weather.noaa.gov/pub/data/' . - "observations/metar/stations/$icao.TXT HTTP/1.0\r\n" . - "If-Modified-Since: Sat, 29 Oct 1994 09:00:00 GMT\r\n" . - "Pragma: no-cache\r\n". - "Cache-Contol: no-cache\r\n\r\n"); - - /* We check the status line */ - if (strpos(fgets($fp, 1024), '200 ')) { - /* Then we seek until we find the empty line between the - * headers and the contents. - */ - do { - $line = fgets($fp, 1024); - } while ($line != "\r\n"); - /* We know now, that the following lines are the contents. */ - unset($file); - while ($line = fgets($fp, 1024)) { - $file[] = $line; - } - fclose($fp); - } + $request = "GET http://$host$location " . $request . + "Host: $host\r\n" . + "Content-Type: text/html\r\n" . + "Connection: Close\r\n\r\n"; + } else { + // allow_url_fopen is often off. + // se we can use this method or curl or shell_exec wget + $fp = fsockopen('weather.noaa.gov', 80); + $request = "GET $location " . $request . + "Host: $host\r\n" . + "Content-Type: text/html\r\n" . + "Connection: Close\r\n\r\n"; + } + if ($fp) { + fputs($fp, $request); + /* We check the status line */ + if (strpos(fgets($fp, 1024), '200 ')) { + /* Then we seek until we find the empty line between the + * headers and the contents. + */ + do { + $line = fgets($fp, 1024); + } while ($line != "\r\n"); + + /* We know now, that the following lines are the contents. */ + $file = array(); + while ($line = fgets($fp, 1024)) { + $file[] = $line; + } + fclose($fp); } - } else { - /* No proxy - we just fetch the file the normal way. */ - /* We use the @file notation here because it might fail. */ - $file = @file('http://weather.noaa.gov/pub/data/' . - "observations/metar/stations/$icao.TXT"); - } - /* Here we test to see if we actually got a METAR. */ - if (is_array($file)) { - + if (isset($file) and is_array($file)) { /* The first line in the file is the date */ $date = trim(array_shift($file)); - /* The remaining lines are the METAR itself. This will merge the * remaining lines into one line by removing new-lines: */ @@ -257,8 +261,7 @@ */ $date[1]--; } - $timestamp = gmmktime($date[3], $date[4], 0, $date[1], - $date[2], $date[0]); + $timestamp = gmmktime($date[3], $date[4], 0, $date[1], $date[2], $date[0]); if (!ereg('[0-9]{6}Z', $metar)) { /* Some reports don't even have a time-part, so we insert the |