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 Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629781 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. Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629784 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. Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629786 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.) Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629796 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. Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629799 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. Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629800 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. Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629802 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!"); } ?> Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629804 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. Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629812 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); Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629822 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 Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629832 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? Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629839 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. Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629843 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 = ''; Link to comment https://forums.phpfreaks.com/topic/122009-script-functioning-locally-but-not-online/#findComment-629844 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.