By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,347 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,347 IT Pros & Developers. It's quick & easy.

Displaying only part of a remote txt file

P: 33
Hello,

This network has helped me greatly on many occassions in the past, this is my first post here as I cannot find the answer elsewhere =/

I am trying to display only the second line (the actual METAR report, not the date) from this text file:

ftp://tgftp.nws.noaa.gov/data/observ...tions/KMEM.TXT

Another problem with this is that my host does not allow URL file access for security reasons I presume. If anyone can help, or just stear me in the right direction I'd appreciate it!

Thanks so much!
Feb 23 '08 #1
Share this Question
Share on Google+
6 Replies


P: 11
flydev -

well i'm really a .net developer and do not have a lot of experiance with php, but if I understand your question correctly, then I understand the logic of what you are trying to accomplish.
first you need to get the data into your application. in .net i would use a web request object to make my request for the file (using the ftp url you provided)... I searched around for a webrequest object for php and could not successfully find what i was looking for.. the closest I found was from the Mojave 3.0.0 api in which has a class registered under requests called WebRequest. the problem there is that I cant find any way to download that file (and related files) - from http://mojave.org.

so when you get your data which will be essentially a string, split it into an array by the carrige return [line feed] and by the look of the text file should be the second index of that array.. now simply plug that into wherever you want.


here is an example of how i would do it in .net

protected void Page_Load(object sender, EventArgs e)
{
FtpWebRequest rq = (FtpWebRequest)WebRequest.Create("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KMEM.TXT");
rq.Method = WebRequestMethods.Ftp.DownloadFile;
WebResponse rsp = rq.GetResponse();
Stream str = rsp.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.ASCII);
Response.Write(sr.ReadToEnd());

}


I'll keep looking for a way to perform a webrequest and post here if i find one.

good luck,
odobo

Hello,

This network has helped me greatly on many occassions in the past, this is my first post here as I cannot find the answer elsewhere =/

I am trying to display only the second line (the actual METAR report, not the date) from this text file:

ftp://tgftp.nws.noaa.gov/data/observ...tions/KMEM.TXT

Another problem with this is that my host does not allow URL file access for security reasons I presume. If anyone can help, or just stear me in the right direction I'd appreciate it!

Thanks so much!
Feb 23 '08 #2

P: 11
ok - maybe that was a little too much for php-- it's actually a lot easier than this.. just do this...

[PHP]$filename = "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KMEM.TXT";
$lineNumber = 1;
if (!($filearray = file ($filename))) {
print "Can't open file $filename";
}
else {
while (list ($line_number, $line_contents) = each ($filearray)) {
// do something with $line_contents...
if($lineNumber==2)
echo $line_contents."<br />";
$lineNumber++;
}
}[/PHP]

-odobo

flydev -

well i'm really a .net developer and do not have a lot of experiance with php, but if I understand your question correctly, then I understand the logic of what you are trying to accomplish.
first you need to get the data into your application. in .net i would use a web request object to make my request for the file (using the ftp url you provided)... I searched around for a webrequest object for php and could not successfully find what i was looking for.. the closest I found was from the Mojave 3.0.0 api in which has a class registered under requests called WebRequest. the problem there is that I cant find any way to download that file (and related files) - from http://mojave.org.

so when you get your data which will be essentially a string, split it into an array by the carrige return [line feed] and by the look of the text file should be the second index of that array.. now simply plug that into wherever you want.


here is an example of how i would do it in .net

protected void Page_Load(object sender, EventArgs e)
{
FtpWebRequest rq = (FtpWebRequest)WebRequest.Create("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KMEM.TXT");
rq.Method = WebRequestMethods.Ftp.DownloadFile;
WebResponse rsp = rq.GetResponse();
Stream str = rsp.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.ASCII);
Response.Write(sr.ReadToEnd());

}


I'll keep looking for a way to perform a webrequest and post here if i find one.

good luck,
odobo
Feb 23 '08 #3

P: 33
odobo, thanks so much for your help! Looking at what you provide, it looks like exactly what I need, and I am sure it would work perfectly, however the method used to retrieve the file is not permitted on my current host, and is not on most hosts I've learned =(

I've learned that NOAA offers similar data via an XML feed, but the same dillema exists of URL access denial from my host. In other words, URL's are not permitted as sources.

Thanks again!

ok - maybe that was a little too much for php-- it's actually a lot easier than this.. just do this...

[PHP]$filename = "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KMEM.TXT";
$lineNumber = 1;
if (!($filearray = file ($filename))) {
print "Can't open file $filename";
}
else {
while (list ($line_number, $line_contents) = each ($filearray)) {
// do something with $line_contents...
if($lineNumber==2)
echo $line_contents."<br />";
$lineNumber++;
}
}[/PHP]

-odobo
Feb 24 '08 #4

P: 33
Hmm, I've dug a little deeper and it appears the default retrieval method uses fopen, and by using CURL, it actually works.

Furthermore, I am not sure if it does this for all files retrieved, but when printing the entire file, it combines it to one line. Perhaps it doesnt recognize line breaks in .txt files? I will continue to work on modifying the script you provided and post the results if i come up with em =)

Thanks!
Feb 24 '08 #5

P: 33
ok, got it!

Turns out it does NOT recognize line breaks after all. The following worked like a champ:

[PHP]<? $ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KMEM.TXT');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

$metar = substr($file_contents, 21); // 21 is the number of characters after 0 to begin parsing
echo $metar;
?>[/PHP]

Even though I didn't end up having to use your script, it sparked a chain of thoughts, im sure you know how that goes ;-) I'll keep your method for future reference. Much appreciated!
Feb 24 '08 #6

Markus
Expert 5K+
P: 6,050
ok, got it!

Turns out it does NOT recognize line breaks after all. The following worked like a champ:

[PHP]<? $ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KMEM.TXT');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

$metar = substr($file_contents, 21); // 21 is the number of characters after 0 to begin parsing
echo $metar;
?>[/PHP]

Even though I didn't end up having to use your script, it sparked a chain of thoughts, im sure you know how that goes ;-) I'll keep your method for future reference. Much appreciated!
I believe line breaks, in .txt files, are known as '\n' - newline characters.
Feb 24 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.