473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Displaying only part of a remote txt file

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
6 2080
odobo
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
odobo
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
flydev
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
flydev
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
flydev
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
6,050 Expert 4TB
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

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

Similar topics

0
by: Google Mike | last post by:
On Linux, I need to ask what your favorite installation options are for a PHP app as a kind of poll. In Part 1, I discussed the Install Wizard preference that you might have. In Part 2, here, I'm...
3
by: Julie | last post by:
I have an html file where I display name,address,zip. It is one per line so it is basicall a list of addresses. But I would like ie6 to force the download prompt of this long list of names...
0
by: haylow | last post by:
Hi I am new to ASP.NET and am working on an application that runs on a webserver. The user will open up the web interface in a browser on their local machine and enter a path to a directory. I...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
20
by: Tim Reynolds | last post by:
Team, I am developing a web service. In testing in on my enw PC, I am expecting to see exceptions thrown appear on my browser. Instead I am getting an HTTP 500 Internal Server Error page and I am...
1
by: Christopher Brandsdal | last post by:
Hi! I have problems displaying a XML-file the way i want on my website. This is the link to the file I have to display(ps: the file is on a remote server, so I have to the file via http or...
13
by: David W. Fenton | last post by:
I've been struggling the last two days with something I thought was very easy, which is to open a web page with a form on it and populate the form with data passed in a query string (either POST or...
38
by: ted | last post by:
I have an old link that was widely distributed. I would now like to put a link on that old page that will go to a new page without displaying anything.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.