CVS: phpweather data_retrieval.php,1.26,1.27 defaults-dist.php,1.12,1...
Brought to you by:
iridium
From: Martin G. <gim...@us...> - 2002-09-20 20:44:07
|
Update of /cvsroot/phpweather/phpweather In directory usw-pr-cvs1:/tmp/cvs-serv27584 Modified Files: data_retrieval.php defaults-dist.php Log Message: This should fix bug 611597 by giving the user the option between using fsockopen() and file() to retrieve the METAR. Index: data_retrieval.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/data_retrieval.php,v retrieving revision 1.26 retrieving revision 1.27 diff -u -3 -r1.26 -r1.27 --- data_retrieval.php 18 Sep 2002 13:24:34 -0000 1.26 +++ data_retrieval.php 20 Sep 2002 20:44:02 -0000 1.27 @@ -127,6 +127,63 @@ $this->decoded_metar = $this->decode_metar(); } } + + + function get_metar_socket($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 */ + $fp = fsockopen($this->properties['proxy_host'], + $this->properties['proxy_port']); + $request = "GET http://$host$location $request" . + "Host: $host\r\n" . + "Content-Type: text/html\r\n" . + "Connection: Close\r\n\r\n"; + } else { + $fp = fsockopen($host, 80); + $request = "GET $location $request" . + "Host: $host\r\n" . + "Content-Type: text/html\r\n" . + "Connection: Close\r\n\r\n"; + } + + $metar_data = false; + + 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. */ + while ($line = fgets($fp, 1024)) { + $metar_data[] = $line; + } + fclose($fp); + } + } + + return $metar_data; + } + + + function get_metar_file($icao) { + $host = 'weather.noaa.gov'; + $location = "/pub/data/observations/metar/stations/$icao.TXT"; + return @file('http://' . $host . $location); + } + /** * Tries to get a METAR from the database. @@ -202,57 +259,18 @@ */ 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 */ - $fp = fsockopen($this->properties['proxy_host'], - $this->properties['proxy_port']); - $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"); + $icao = $this->get_icao(); + $func = $this->properties['fetch_function']; + $metar_data = $this->$func($icao); - /* We know now, that the following lines are the contents. */ - $file = array(); - while ($line = fgets($fp, 1024)) { - $file[] = $line; - } - fclose($fp); - } - } /* Here we test to see if we actually got a METAR. */ - if (isset($file) and is_array($file)) { + if (!empty($metar_data)) { /* The first line in the file is the date */ - $date = trim(array_shift($file)); + $date = trim(array_shift($metar_data)); /* The remaining lines are the METAR itself. This will merge the * remaining lines into one line by removing new-lines: */ - $metar = ereg_replace("[\n\r ]+", ' ', trim(implode(' ', $file))); + $metar = ereg_replace("[\n\r ]+", ' ', trim(implode(' ', $metar_data))); $date = explode(':', strtr($date, '/ ', '::')); if ($date[2] > gmdate('j')) { @@ -261,7 +279,8 @@ */ $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 Index: defaults-dist.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/defaults-dist.php,v retrieving revision 1.12 retrieving revision 1.13 diff -u -3 -r1.12 -r1.13 --- defaults-dist.php 12 Apr 2002 22:00:27 -0000 1.12 +++ defaults-dist.php 20 Sep 2002 20:44:02 -0000 1.13 @@ -24,6 +24,7 @@ $this->properties['use_proxy'] = false; $this->properties['proxy_host'] = ''; $this->properties['proxy_port'] = 3128; +$this->properties['fetch_function'] = 'get_metar_socket'; $this->properties['db_hostname'] = ''; /* pw_db_common.php */ $this->properties['db_database'] = ''; |