Displaying only part of a remote txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flydev
    New Member
    • Feb 2008
    • 33

    Displaying only part of a remote txt file

    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!
  • odobo
    New Member
    • Feb 2008
    • 11

    #2
    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(objec t sender, EventArgs e)
    {
    FtpWebRequest rq = (FtpWebRequest) WebRequest.Crea te("ftp://tgftp.nws.noaa. gov/data/observations/metar/stations/KMEM.TXT");
    rq.Method = WebRequestMetho ds.Ftp.Download File;
    WebResponse rsp = rq.GetResponse( );
    Stream str = rsp.GetResponse Stream();
    StreamReader sr = new StreamReader(st r, System.Text.Enc oding.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

    Originally posted by flydev
    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!

    Comment

    • odobo
      New Member
      • Feb 2008
      • 11

      #3
      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

      Originally posted by 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(objec t sender, EventArgs e)
      {
      FtpWebRequest rq = (FtpWebRequest) WebRequest.Crea te("ftp://tgftp.nws.noaa. gov/data/observations/metar/stations/KMEM.TXT");
      rq.Method = WebRequestMetho ds.Ftp.Download File;
      WebResponse rsp = rq.GetResponse( );
      Stream str = rsp.GetResponse Stream();
      StreamReader sr = new StreamReader(st r, System.Text.Enc oding.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

      Comment

      • flydev
        New Member
        • Feb 2008
        • 33

        #4
        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!

        Originally posted by odobo
        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
        Last edited by flydev; Feb 24 '08, 05:09 AM. Reason: more information

        Comment

        • flydev
          New Member
          • Feb 2008
          • 33

          #5
          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!

          Comment

          • flydev
            New Member
            • Feb 2008
            • 33

            #6
            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_RETURNT RANSFER, 1);
            curl_setopt ($ch, CURLOPT_CONNECT TIMEOUT, $timeout);
            $file_contents = curl_exec($ch);
            curl_close($ch) ;

            $metar = substr($file_co ntents, 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!

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by flydev
              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_RETURNT RANSFER, 1);
              curl_setopt ($ch, CURLOPT_CONNECT TIMEOUT, $timeout);
              $file_contents = curl_exec($ch);
              curl_close($ch) ;

              $metar = substr($file_co ntents, 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.

              Comment

              Working...