mcatalf0221 Posted August 30, 2008 Share Posted August 30, 2008 Hello, Some months ago you guys assisted me in a PHP script that would take a text file, trim up the formatting, and echo it on my page. The following is the code: <?php $station = 'KOQU'; $metar = get_metar($station); function get_metar($station) { $fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$station.TXT"; $metar = ''; $fileData = @file($fileName); // or die('Data not available'); if ($fileData != false) { list($i, $date) = each($fileData); $utc = strtotime(trim($date)); while (list($i, $line) = each($fileData)) { $metar .= ' ' . trim($line); } $metar = trim(str_replace(' ', ' ', $metar)); } return $metar; } ?> <html> <body> <?php echo $metar; ?> </body> </html> The problem is that the script works on local servers (Abyss, etc.), but when I upload it to my website, the output of the weather report is blank (i.e. http://cindtronix.com/metar.php). I've researched this via search and have tried checking the character encoding, PHP version, etc. -- without luck. Is a problem with the code itself causing this to occur? Thank you! mcatalf0221 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Add the following lines to the very top of your script: <?php ini_set('display_errors', 1); error_reporting(E_ALL); ?> Quote Link to comment Share on other sites More sharing options...
mcatalf0221 Posted August 30, 2008 Author Share Posted August 30, 2008 Done. Not seeing any error reporting on my page, however. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Check your server logs to see if there were any errors. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 30, 2008 Share Posted August 30, 2008 The @ in front of the file() function would suppress the display or logging of any errors, such as allow_url_fopen being off. No code should contain any @ (on a development system, display_errors should be on and on a live server display_errors should be off.) Quote Link to comment Share on other sites More sharing options...
mcatalf0221 Posted August 30, 2008 Author Share Posted August 30, 2008 Thanks PFMaBiSmAd ... here are the errors: Warning: file() [function.file]: URL file-access is disabled in the server configuration in /hermes/web09/b870/as.cindtron/public_html/metar.php on line 11 Warning: file(http://weather.noaa.gov/pub/data/observations/metar/stations/KOQU.TXT) [function.file]: failed to open stream: no suitable wrapper could be found in /hermes/web09/b870/as.cindtron/public_html/metar.php on line 11 I'm not sure how to interpret these. Any diagnosis would be much appreciated. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Just as he had thought, allow_url_fopen is off, which means you can't directly open files through the HTTP wrapper. You'll need to use cURL. Quote Link to comment Share on other sites More sharing options...
mcatalf0221 Posted August 30, 2008 Author Share Posted August 30, 2008 Thanks DarkWater. How should I use this cURL on my server? I'm completely unfamiliar with the term. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 First, add this code to the top to make sure that cURL is enabled: <?php if (function_exists('curl_init')) { die("cURL enabled!"); } else { die("cURL not enabled!"); } ?> Quote Link to comment Share on other sites More sharing options...
mcatalf0221 Posted August 30, 2008 Author Share Posted August 30, 2008 My page now says "cURL enabled!" only. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Good. Remove that little section of code that I just told you put in, and replace these lines: $fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$station.TXT"; $metar = ''; $fileData = @file($fileName); With: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://weather.noaa.gov/pub/data/observations/metar/stations/$station.TXT"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $output = curl_exec($ch); $fileData = explode("\n", $output); Quote Link to comment Share on other sites More sharing options...
mcatalf0221 Posted August 30, 2008 Author Share Posted August 30, 2008 Awesome! Looks like I'm getting closer. Here's what I have now: Notice: Undefined variable: metar in /hermes/web09/b870/as.cindtron/public_html/metar.php on line 19 KOQU 301750Z 08003KT 6SM BR BKN005 OVC015 23/ A3005 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 What's on line 19? And it shows all of the correct data now, right? Just has that error on top? Quote Link to comment Share on other sites More sharing options...
mcatalf0221 Posted August 30, 2008 Author Share Posted August 30, 2008 Line 19: $metar .= ' ' . trim($line); Yes, that second line is exactly what I'm looking for. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Right underneath the $fileData = explode(); line, you can try adding: $metar = ''; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.