Reading a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VooDoo

    Reading a file

    Hi,
    I have a Temperature sensor with internal web server that provide the data
    in a text format.
    When quering the ip(ttp://192.168.10.220/temp) of the sensor i get that kind
    of reply:
    Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
    I woud like to import the result into my mysql database.
    What is the best way to do this?
    Should i use a fopen() ?
    I have try with this but i don't know how get just the value data....

    Is there a simple way of achieving this?
    thanks for your help.
    VooDoo


  • Erwin Moller

    #2
    Re: Reading a file

    VooDoo wrote:
    [color=blue]
    > Hi,
    > I have a Temperature sensor with internal web server that provide the data
    > in a text format.
    > When quering the ip(ttp://192.168.10.220/temp) of the sensor i get that
    > kind of reply:
    > Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
    > I woud like to import the result into my mysql database.
    > What is the best way to do this?
    > Should i use a fopen() ?
    > I have try with this but i don't know how get just the value data....
    >
    > Is there a simple way of achieving this?
    > thanks for your help.
    > VooDoo[/color]

    Hi Voodoo,

    1) just fopen the file
    $handle = fopen("http://192.168.10.220/temp", "r");

    2) read the line and store it in a var.
    $myTempLine = fgets($handle);

    3) close the file.
    fclose($handle) ;

    4) explode the var with | as seperator. (check www.php.net, find explode)
    It will return an array of all elements.
    $myElements = explode("|", $myTempLine );

    5) store them in your database.

    Good luck,

    Regards,
    Erwin Moller

    Comment

    • Michael Fesser

      #3
      Re: Reading a file

      .oO(VooDoo)
      [color=blue]
      >I have a Temperature sensor with internal web server that provide the data
      >in a text format.
      >When quering the ip(ttp://192.168.10.220/temp) of the sensor i get that kind
      >of reply:
      >Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
      >I woud like to import the result into my mysql database.
      >What is the best way to do this?
      >Should i use a fopen() ?
      >I have try with this but i don't know how get just the value data....[/color]

      $data = file_get_conten ts('http://192.168.10.220/temp');
      preg_match_all( '#probe (\d+)\|([^|]+)#i', $data, $m);

      PHP5:
      $result = array_combine($ m[1], $m[2]);

      PHP4:
      $result = array();
      for ($i = 0; $i < count($m[1]); $i++) {
      $result[$m[1][$i]] = $m[2][$i];
      }

      The result:

      Array
      (
      [1] => 82.2
      [2] => 80.8
      [3] => -99.9
      [4] => -99.9
      )

      HTH
      Micha

      Comment

      • VooDoo

        #4
        Re: Reading a file

        Hi guys!
        Thanks a lot for your help i will try all your solution and see wich one i
        like the most.
        I really appreciated your fast help.
        Rgds,
        VooDoo
        "Michael Fesser" <netizen@gmx.ne t> a écrit dans le message de
        news:c2b4i0tn9i spq32oien8iv8ck j8qopf3v3@4ax.c om...[color=blue]
        > .oO(VooDoo)
        >[color=green]
        > >I have a Temperature sensor with internal web server that provide the[/color][/color]
        data[color=blue][color=green]
        > >in a text format.
        > >When quering the ip(ttp://192.168.10.220/temp) of the sensor i get that[/color][/color]
        kind[color=blue][color=green]
        > >of reply:
        > >Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
        > >I woud like to import the result into my mysql database.
        > >What is the best way to do this?
        > >Should i use a fopen() ?
        > >I have try with this but i don't know how get just the value data....[/color]
        >
        > $data = file_get_conten ts('http://192.168.10.220/temp');
        > preg_match_all( '#probe (\d+)\|([^|]+)#i', $data, $m);
        >
        > PHP5:
        > $result = array_combine($ m[1], $m[2]);
        >
        > PHP4:
        > $result = array();
        > for ($i = 0; $i < count($m[1]); $i++) {
        > $result[$m[1][$i]] = $m[2][$i];
        > }
        >
        > The result:
        >
        > Array
        > (
        > [1] => 82.2
        > [2] => 80.8
        > [3] => -99.9
        > [4] => -99.9
        > )
        >
        > HTH
        > Micha[/color]


        Comment

        • VooDoo

          #5
          Re: Reading a file

          Hi,
          I have try yours first and other reply code also but none are working.
          All the values are blank. the array seems always to be empty...
          Any idea?
          "Erwin Moller"
          <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> a écrit dans
          le message de news:41221850$0 $37789$e4fe514c @news.xs4all.nl ...[color=blue]
          > VooDoo wrote:
          >[color=green]
          > > Hi,
          > > I have a Temperature sensor with internal web server that provide the[/color][/color]
          data[color=blue][color=green]
          > > in a text format.
          > > When quering the ip(ttp://192.168.10.220/temp) of the sensor i get that
          > > kind of reply:
          > > Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
          > > I woud like to import the result into my mysql database.
          > > What is the best way to do this?
          > > Should i use a fopen() ?
          > > I have try with this but i don't know how get just the value data....
          > >
          > > Is there a simple way of achieving this?
          > > thanks for your help.
          > > VooDoo[/color]
          >
          > Hi Voodoo,
          >
          > 1) just fopen the file
          > $handle = fopen("http://192.168.10.220/temp", "r");
          >
          > 2) read the line and store it in a var.
          > $myTempLine = fgets($handle);
          >
          > 3) close the file.
          > fclose($handle) ;
          >
          > 4) explode the var with | as seperator. (check www.php.net, find explode)
          > It will return an array of all elements.
          > $myElements = explode("|", $myTempLine );
          >
          > 5) store them in your database.
          >
          > Good luck,
          >
          > Regards,
          > Erwin Moller
          >[/color]


          Comment

          • Erwin Moller

            #6
            Re: Reading a file

            VooDoo wrote:
            [color=blue]
            > Hi,
            > I have try yours first and other reply code also but none are working.
            > All the values are blank. the array seems always to be empty...
            > Any idea?[/color]

            Shame. :-(
            [color=blue][color=green]
            >> 1) just fopen the file
            >> $handle = fopen("http://192.168.10.220/temp", "r");
            >>
            >> 2) read the line and store it in a var.
            >> $myTempLine = fgets($handle);[/color][/color]

            If you print out the $myTempLine, what do you see?
            Is it empty?

            Regards,
            Erwin Moller

            Comment

            • VooDoo

              #7
              Re: Reading a file

              yes...
              "Erwin Moller"
              <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> a écrit dans
              le message de news:41234c22$0 $78772$e4fe514c @news.xs4all.nl ...[color=blue]
              > VooDoo wrote:
              >[color=green]
              > > Hi,
              > > I have try yours first and other reply code also but none are working.
              > > All the values are blank. the array seems always to be empty...
              > > Any idea?[/color]
              >
              > Shame. :-(
              >[color=green][color=darkred]
              > >> 1) just fopen the file
              > >> $handle = fopen("http://192.168.10.220/temp", "r");
              > >>
              > >> 2) read the line and store it in a var.
              > >> $myTempLine = fgets($handle);[/color][/color]
              >
              > If you print out the $myTempLine, what do you see?
              > Is it empty?
              >
              > Regards,
              > Erwin Moller[/color]


              Comment

              • Michael Fesser

                #8
                Re: Reading a file

                .oO(VooDoo)
                [color=blue]
                >I have try yours first and other reply code also but none are working.
                >All the values are blank. the array seems always to be empty...
                >Any idea?[/color]

                Check your php.ini:

                Is allow_url_fopen enabled?
                Is error_reporting set to E_ALL?

                Micha

                Comment

                • VooDoo

                  #9
                  Re: Reading a file

                  yes for both.
                  If i use the url http://192.168.10.220/
                  wich is the main page for the sensor view the it works....
                  The page http://192.168.10.220/temp/ only contain this:
                  Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                  with no html code at all, is that the problem???
                  The main page is full off html and other code....
                  Thanks for your help.
                  VooDoo
                  "Michael Fesser" <netizen@gmx.ne t> a écrit dans le message de
                  news:t0n6i0dr36 v0mehkncuaderg9 r9eij2fsm@4ax.c om...[color=blue]
                  > .oO(VooDoo)
                  >[color=green]
                  > >I have try yours first and other reply code also but none are working.
                  > >All the values are blank. the array seems always to be empty...
                  > >Any idea?[/color]
                  >
                  > Check your php.ini:
                  >
                  > Is allow_url_fopen enabled?
                  > Is error_reporting set to E_ALL?
                  >
                  > Micha[/color]


                  Comment

                  • Erwin Moller

                    #10
                    Re: Reading a file

                    VooDoo wrote:
                    [color=blue]
                    > yes...[/color]

                    Ok,

                    Probably that internal webserver send a lot back. Many lines.
                    (In my example I expected only one line back.)

                    Let's check that first by getting the WHOLE response in:

                    Try this first:
                    (Straight from http://nl.php.net/manual/en/function.file.php)


                    $lines = file('http://192.168.10.220/temp');

                    foreach ($lines as $line_num => $line) {
                    echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) . "<br>\n";
                    }


                    do you get a response that makes sense?
                    If so, try to find the linenumber that contains the information you need.

                    Keep me informed. We'll fix this. :-)

                    Regards,
                    Erwin

                    Comment

                    • VooDoo

                      #11
                      Re: Reading a file

                      Thanks erwin.
                      No apparently the web server only send out this line:
                      Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                      nothing else, no html code at all.
                      If i use the default page of the server (192.168.10.220 ) where there is a
                      full of other caracter, then it works fine...
                      the constructo said that the page192.168.10. 220/temp has been designed to
                      fit custom application, and it just provided the value of the different
                      probe...
                      i try out your new code and let you know...

                      "Erwin Moller"
                      <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> a écrit dans
                      le message de news:4123556a$0 $14941$e4fe514c @news.xs4all.nl ...[color=blue]
                      > VooDoo wrote:
                      >[color=green]
                      > > yes...[/color]
                      >
                      > Ok,
                      >
                      > Probably that internal webserver send a lot back. Many lines.
                      > (In my example I expected only one line back.)
                      >
                      > Let's check that first by getting the WHOLE response in:
                      >
                      > Try this first:
                      > (Straight from http://nl.php.net/manual/en/function.file.php)
                      >
                      >
                      > $lines = file('http://192.168.10.220/temp');
                      >
                      > foreach ($lines as $line_num => $line) {
                      > echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) .[/color]
                      "<br>\n";[color=blue]
                      > }
                      >
                      >
                      > do you get a response that makes sense?
                      > If so, try to find the linenumber that contains the information you need.
                      >
                      > Keep me informed. We'll fix this. :-)
                      >
                      > Regards,
                      > Erwin
                      >[/color]


                      Comment

                      • VooDoo

                        #12
                        Re: Reading a file

                        same thing with thte new code..... :(
                        "VooDoo" <cr.stepan@ifra nce.com> a écrit dans le message de
                        news:cfvjq3$96f $1@reader1.imag inet.fr...[color=blue]
                        > Thanks erwin.
                        > No apparently the web server only send out this line:
                        > Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                        > nothing else, no html code at all.
                        > If i use the default page of the server (192.168.10.220 ) where there is a
                        > full of other caracter, then it works fine...
                        > the constructo said that the page192.168.10. 220/temp has been designed to
                        > fit custom application, and it just provided the value of the different
                        > probe...
                        > i try out your new code and let you know...
                        >
                        > "Erwin Moller"
                        > <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> a écrit[/color]
                        dans[color=blue]
                        > le message de news:4123556a$0 $14941$e4fe514c @news.xs4all.nl ...[color=green]
                        > > VooDoo wrote:
                        > >[color=darkred]
                        > > > yes...[/color]
                        > >
                        > > Ok,
                        > >
                        > > Probably that internal webserver send a lot back. Many lines.
                        > > (In my example I expected only one line back.)
                        > >
                        > > Let's check that first by getting the WHOLE response in:
                        > >
                        > > Try this first:
                        > > (Straight from http://nl.php.net/manual/en/function.file.php)
                        > >
                        > >
                        > > $lines = file('http://192.168.10.220/temp');
                        > >
                        > > foreach ($lines as $line_num => $line) {
                        > > echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) .[/color]
                        > "<br>\n";[color=green]
                        > > }
                        > >
                        > >
                        > > do you get a response that makes sense?
                        > > If so, try to find the linenumber that contains the information you[/color][/color]
                        need.[color=blue][color=green]
                        > >
                        > > Keep me informed. We'll fix this. :-)
                        > >
                        > > Regards,
                        > > Erwin
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Erwin Moller

                          #13
                          Re: Reading a file

                          VooDoo wrote:
                          [color=blue]
                          > same thing with thte new code..... :(
                          > "VooDoo" <cr.stepan@ifra nce.com> a �rit dans le message de
                          > news:cfvjq3$96f $1@reader1.imag inet.fr...[color=green]
                          >> Thanks erwin.
                          >> No apparently the web server only send out this line:
                          >> Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                          >> nothing else, no html code at all.
                          >> If i use the default page of the server (192.168.10.220 ) where there is a
                          >> full of other caracter, then it works fine...
                          >> the constructo said that the page192.168.10. 220/temp has been designed to
                          >> fit custom application, and it just provided the value of the different
                          >> probe...
                          >> i try out your new code and let you know...[/color][/color]

                          Hi VooDoo,

                          Let me recap the situation.
                          Just to be sure we understand each other.

                          You say:[color=blue][color=green]
                          >> No apparently the web server only send out this line:
                          >> Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9[/color][/color]

                          Question: How do you know this?

                          Is this what our testscript also produces?
                          I mean:
                          $lines = file('http://192.168.10.220/temp');
                          foreach ($lines as $line_num => $line) {
                          echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) . "<br>\n";
                          }

                          produces nothing?

                          And when you visit the same address with your webbrowser you get:
                          Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9

                          ???

                          Is that true?

                          Because when that is the case, PHP somehow cannot grab the output produced
                          by that internal webserver, which is strange.

                          Can you test this by trying to query google, like this:

                          $lines = file('http://www.google.com' );
                          foreach ($lines as $line_num => $line) {
                          echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) . "<br>\n";
                          }

                          Does that produce many lines of code?

                          Regards,
                          Erwin Moller

                          Comment

                          • VooDoo

                            #14
                            Re: Reading a file

                            reply below
                            "Erwin Moller"
                            <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> a écrit dans
                            le message de news:41236421$0 $65124$e4fe514c @news.xs4all.nl ...[color=blue]
                            > VooDoo wrote:
                            >[color=green]
                            > > same thing with thte new code..... :(
                            > > "VooDoo" <cr.stepan@ifra nce.com> a ?rit dans le message de
                            > > news:cfvjq3$96f $1@reader1.imag inet.fr...[color=darkred]
                            > >> Thanks erwin.
                            > >> No apparently the web server only send out this line:
                            > >> Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                            > >> nothing else, no html code at all.
                            > >> If i use the default page of the server (192.168.10.220 ) where there is[/color][/color][/color]
                            a[color=blue][color=green][color=darkred]
                            > >> full of other caracter, then it works fine...
                            > >> the constructo said that the page192.168.10. 220/temp has been designed[/color][/color][/color]
                            to[color=blue][color=green][color=darkred]
                            > >> fit custom application, and it just provided the value of the different
                            > >> probe...
                            > >> i try out your new code and let you know...[/color][/color]
                            >
                            > Hi VooDoo,
                            >
                            > Let me recap the situation.
                            > Just to be sure we understand each other.
                            >
                            > You say:[color=green][color=darkred]
                            > >> No apparently the web server only send out this line:
                            > >> Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9[/color][/color]
                            >
                            > Question: How do you know this?[/color]
                            That is what i get when typing these url on IE:
                            http://192.168.10.220/temp or http://192.168.10.220/temp/[color=blue]
                            >
                            > Is this what our testscript also produces?
                            > I mean:
                            > $lines = file('http://192.168.10.220/temp');
                            > foreach ($lines as $line_num => $line) {
                            > echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) .[/color]
                            "<br>\n";[color=blue]
                            > }
                            >
                            > produces nothing?[/color]
                            Yes ![color=blue]
                            > And when you visit the same address with your webbrowser you get:
                            > Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                            >
                            > ???
                            >
                            > Is that true?[/color]
                            YES[color=blue]
                            > Because when that is the case, PHP somehow cannot grab the output produced
                            > by that internal webserver, which is strange.
                            >
                            > Can you test this by trying to query google, like this:
                            >
                            > $lines = file('http://www.google.com' );
                            > foreach ($lines as $line_num => $line) {
                            > echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) .[/color]
                            "<br>\n";[color=blue]
                            > }
                            >
                            > Does that produce many lines of code?
                            > YES that works fine....[/color]
                            It's very strange, i don't what's wrong...
                            I have contacted the support of the temp sensor to see if it's a issue for
                            them...
                            But as far as i get the right content on that page: 192.168.200.10/temp i
                            imagine they will say it's working...
                            It's also strange that your code is working on the main page....
                            ((192.168.200.1 0)
                            Any more ideas?
                            Thanks a lot for your support!
                            Cedric



                            Comment

                            • Erwin Moller

                              #15
                              Re: Reading a file

                              VooDoo wrote:
                              [color=blue]
                              > reply below
                              > "Erwin Moller"
                              > <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> a �rit
                              > dans le message de news:41236421$0 $65124$e4fe514c @news.xs4all.nl ...[color=green]
                              >> VooDoo wrote:
                              >>[color=darkred]
                              >> > same thing with thte new code..... :(
                              >> > "VooDoo" <cr.stepan@ifra nce.com> a ?rit dans le message de
                              >> > news:cfvjq3$96f $1@reader1.imag inet.fr...
                              >> >> Thanks erwin.
                              >> >> No apparently the web server only send out this line:
                              >> >> Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                              >> >> nothing else, no html code at all.
                              >> >> If i use the default page of the server (192.168.10.220 ) where there
                              >> >> is[/color][/color]
                              > a[color=green][color=darkred]
                              >> >> full of other caracter, then it works fine...
                              >> >> the constructo said that the page192.168.10. 220/temp has been designed[/color][/color]
                              > to[color=green][color=darkred]
                              >> >> fit custom application, and it just provided the value of the
                              >> >> different probe...
                              >> >> i try out your new code and let you know...[/color]
                              >>
                              >> Hi VooDoo,
                              >>
                              >> Let me recap the situation.
                              >> Just to be sure we understand each other.
                              >>
                              >> You say:[color=darkred]
                              >> >> No apparently the web server only send out this line:
                              >> >> Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9[/color]
                              >>
                              >> Question: How do you know this?[/color]
                              > That is what i get when typing these url on IE:
                              > http://192.168.10.220/temp or http://192.168.10.220/temp/[color=green]
                              >>
                              >> Is this what our testscript also produces?
                              >> I mean:
                              >> $lines = file('http://192.168.10.220/temp');
                              >> foreach ($lines as $line_num => $line) {
                              >> echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) .[/color]
                              > "<br>\n";[color=green]
                              >> }
                              >>
                              >> produces nothing?[/color]
                              > Yes ![color=green]
                              >> And when you visit the same address with your webbrowser you get:
                              >> Probe 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9
                              >>
                              >> ???
                              >>
                              >> Is that true?[/color]
                              > YES[color=green]
                              >> Because when that is the case, PHP somehow cannot grab the output
                              >> produced by that internal webserver, which is strange.
                              >>
                              >> Can you test this by trying to query google, like this:
                              >>
                              >> $lines = file('http://www.google.com' );
                              >> foreach ($lines as $line_num => $line) {
                              >> echo "Line #<b>{$line_num} </b> : " . htmlspecialchar s($line) .[/color]
                              > "<br>\n";[color=green]
                              >> }
                              >>
                              >> Does that produce many lines of code?
                              >> YES that works fine....[/color]
                              > It's very strange, i don't what's wrong...
                              > I have contacted the support of the temp sensor to see if it's a issue for
                              > them...
                              > But as far as i get the right content on that page: 192.168.200.10/temp
                              > i imagine they will say it's working...
                              > It's also strange that your code is working on the main page....
                              > ((192.168.200.1 0)
                              > Any more ideas?
                              > Thanks a lot for your support!
                              > Cedric[/color]


                              Hi Cedric,

                              This is strange indeed!
                              What *could* produce this behaviour is a possible session started by:
                              192.168.200.10

                              which sets a cookie.

                              192.168.200.10/temp TEST for that cookie, and when it is not there, it
                              refuses to answer.
                              (I am guessing)

                              Try this:
                              1) In your browser: delete all content AND cookies.
                              2) restart your browser
                              3) go DIRECTLY to 192.168.200.10/temp without visiting 192.168.200.10 first.

                              Do you get an answer?

                              Regards,
                              Erwin Moller



                              Comment

                              Working...