trouble using variable in fopen

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

    #1

    trouble using variable in fopen

    For my personal use I am accessing a railway timetable page, parsing its
    contents and then sending brief relevant information as the subject line of
    an email to be read on a mobile phone.
    The problem comes when I try to parse a second page, the url of which, I
    have derived from an initial page.
    I cannot go directly to the second (target) page but from the initial page
    derive the url and put it in a string called $strTarget

    In one particular instance $strTarget reads thus:


    If I hard code this string like this
    $fp =
    fopen("http://www.livedepartu reboards.co.uk/ldb/train.aspx?T=FR NCMB++&J=137062 6&R=0",
    'r');
    then the target page opens ok and conveys the timetable information.
    However if, as I have to do, I code it using the variable $strTarget like
    this
    $fp = fopen($strTarge t, 'r');
    then the page that opens is always the timetable site's default page for the
    message,
    "Details for the requested train are no longer available."

    I have checked and double checked to make sure this is indeed what is
    happening.
    Can anyone explain what is going on here?
    Thanks
    Bill


  • CJ Llewellyn

    #2
    Re: trouble using variable in fopen

    "Bill" <bill@spamsucks .com> wrote in message
    news:4Ltsd.2214 2$9A.376710@new s.xtra.co.nz...[color=blue]
    > For my personal use I am accessing a railway timetable page, parsing its
    > contents and then sending brief relevant information as the subject line[/color]
    of[color=blue]
    > an email to be read on a mobile phone.
    > The problem comes when I try to parse a second page, the url of which, I
    > have derived from an initial page.
    > I cannot go directly to the second (target) page but from the initial page
    > derive the url and put it in a string called $strTarget
    >
    > In one particular instance $strTarget reads thus:
    >[/color]
    http://www.livedepartureboards.co.uk...&J=1370626&R=0[color=blue]
    >
    > If I hard code this string like this
    > $fp =
    >[/color]
    fopen("http://www.livedepartu reboards.co.uk/ldb/train.aspx?T=FR NCMB++&J=1370
    626&R=0",[color=blue]
    > 'r');
    > then the target page opens ok and conveys the timetable information.
    > However if, as I have to do, I code it using the variable $strTarget like
    > this[/color]

    echo "[$strTarget]<br>\n";
    [color=blue]
    > $fp = fopen($strTarge t, 'r');[/color]

    Your parsing routine cannot be working as well as you are expecting it to
    work.


    Comment

    • Bill

      #3
      Re: trouble using variable in fopen


      "CJ Llewellyn" <invalid@exampl e.con> wrote in message
      news:cou2re$3b3 $1@slavica.ukpo st.com...[color=blue]
      > "Bill" <bill@spamsucks .com> wrote in message
      > news:4Ltsd.2214 2$9A.376710@new s.xtra.co.nz...[color=green]
      >> For my personal use I am accessing a railway timetable page, parsing its
      >> contents and then sending brief relevant information as the subject line[/color]
      > of[color=green]
      >> an email to be read on a mobile phone.
      >> The problem comes when I try to parse a second page, the url of which, I
      >> have derived from an initial page.
      >> I cannot go directly to the second (target) page but from the initial
      >> page
      >> derive the url and put it in a string called $strTarget
      >>
      >> In one particular instance $strTarget reads thus:
      >>[/color]
      > http://www.livedepartureboards.co.uk...&J=1370626&R=0[color=green]
      >>
      >> If I hard code this string like this
      >> $fp =
      >>[/color]
      > fopen("http://www.livedepartu reboards.co.uk/ldb/train.aspx?T=FR NCMB++&J=1370
      > 626&R=0",[color=green]
      >> 'r');
      >> then the target page opens ok and conveys the timetable information.
      >> However if, as I have to do, I code it using the variable $strTarget like
      >> this[/color]
      >
      > echo "[$strTarget]<br>\n";
      >[color=green]
      >> $fp = fopen($strTarge t, 'r');[/color]
      >
      > Your parsing routine cannot be working as well as you are expecting it to
      > work.[/color]
      It was echo $strTarget that gave me
      http://www.livedepartureboards.co.uk...70626&R=0which I then used to hard code the function

      Comment

      • Janwillem Borleffs

        #4
        Re: trouble using variable in fopen

        Bill wrote:[color=blue]
        > http://www.livedepartureboards.co.uk...&J=1370626&R=0
        > which I then used to hard code the function[/color]

        Even when I paste the URL in my browser, I'm getting an error.


        JW



        Comment

        • Bill

          #5
          Re: trouble using variable in fopen


          "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
          news:41b2e6a8$0 $18262$18b6e80@ news.euronet.nl ...[color=blue]
          > Bill wrote:[color=green]
          >> http://www.livedepartureboards.co.uk...&J=1370626&R=0
          >> which I then used to hard code the function[/color]
          >
          > Even when I paste the URL in my browser, I'm getting an error.
          > JW[/color]

          That is because the url is for a page giving details of a specific train.
          The train it referred to has run long ago.so you now get the message that
          the page is no longer available.
          However at the time I ran the test I assure you that the string when hard
          coded opened the required page
          but when it was contained in a variable it opened a default "details no
          longer available" page.
          I have checked and double checked this.
          Immediately after the variable routed me to the default page I could paste
          the url into my browser and get the page I wanted, and also get it
          programmaticall y by hard coding the string.

          Thank you for taking the time to check out my problem.
          In general terms I am asking:
          Is there any way php treats a variable differently from a hard coded string
          when used in a function?



          Comment

          • Janwillem Borleffs

            #6
            Re: trouble using variable in fopen

            Bill wrote:[color=blue]
            > Thank you for taking the time to check out my problem.
            > In general terms I am asking:
            > Is there any way php treats a variable differently from a hard coded
            > string when used in a function?[/color]

            No, but the problem can be related to livedeparturebo ards.co.uk expecting
            browser specific headers, which are not send with the fopen function.

            When these headers aren't received, their script can be instructed to abort
            the operation.

            Try to use fsockopen instead of fopen, which is capable of sending headers
            with the aid of fputs. The following example sends an user agent header
            along the request:

            $host = 'ivedeparturebo ards.co.uk';
            $post = 80;
            $path = '/ldb/train.aspx?T=FR NCMB++&J=137062 6&R=0';
            $fp = fsockopen($host , $port);
            fputs($fp, "GET $path HTTP/1.0\r\n");
            fputs($fp, "Host: $host\r\n");
            fputs($fp, "User-Agent: {$_SERVER['HTTP_USER_AGEN T']}\r\n");
            fputs($fp, "\r\n");

            fpassthru($fp);
            fclose($fp);


            JW



            Comment

            • Bill

              #7
              Re: trouble using variable in fopen

              > Try to use fsockopen instead of fopen, which is capable of sending headers[color=blue]
              > with the aid of fputs. The following example sends an user agent header
              > along the request:
              >
              > $host = 'ivedeparturebo ards.co.uk';
              > $post = 80;
              > $path = '/ldb/train.aspx?T=FR NCMB++&J=137062 6&R=0';
              > $fp = fsockopen($host , $port);
              > fputs($fp, "GET $path HTTP/1.0\r\n");
              > fputs($fp, "Host: $host\r\n");
              > fputs($fp, "User-Agent: {$_SERVER['HTTP_USER_AGEN T']}\r\n");
              > fputs($fp, "\r\n");
              >
              > fpassthru($fp);
              > fclose($fp);
              >
              >
              > JW[/color]

              Thanks for this detailed help.
              I am afraid I now have problems with fsockopen()
              When I try the above with $host = 'livedepartureb oards.co.uk'; I get

              Warning: fsockopen() [function.fsocko pen]: unable to connect to
              livedeparturebo ards.co.uk:0 (Failed to parse address
              "livedepartureb oards.co.uk")

              I have tried numerous variations for $host but they all give me the above
              error or

              Warning: fsockopen() [function.fsocko pen]: php_network_get addresses:
              getaddrinfo failed: No address associated with hostname

              In case it is relevant stream_get_tran sports() returns : Array ( [0] =>
              tcp [1] => udp [2] => unix [3] => udg )


              Comment

              • Janwillem Borleffs

                #8
                Re: trouble using variable in fopen

                Bill wrote:[color=blue]
                > Thanks for this detailed help.
                > I am afraid I now have problems with fsockopen()
                > When I try the above with $host = 'livedepartureb oards.co.uk'; I get
                >
                > Warning: fsockopen() [function.fsocko pen]: unable to connect to
                > livedeparturebo ards.co.uk:0 (Failed to parse address
                > "livedepartureb oards.co.uk")
                >[/color]

                That's because the first two lines contain errors. Change them into the
                following:

                $host = 'www.livedepart ureboards.co.uk ';
                $port = 80;

                However, when you run the code, you will notice an object moved message. The
                following code also deals with this:

                <?php

                $host = 'www.livedepart ureboards.co.uk ';
                $port = 80;
                $path = '/ldb/train.aspx?T=FR NCMB++&J=137062 6&R=0';
                $break = false;

                while (true) {
                $fp = fsockopen($host , $port);
                fputs($fp, "GET $path HTTP/1.0\r\n");
                fputs($fp, "Host: $host\r\n");
                fputs($fp, "User-Agent: {$_SERVER['HTTP_USER_AGEN T']}\r\n");
                fputs($fp, "\r\n");

                // Get the response code
                $response = (int) strstr(fgets($f p, 1024), " ");

                if (($response != 301) && ($response != 302)) {
                fpassthru($fp);
                $break = true;
                } else {
                while (!preg_match("/^Location:(.+)$/i",
                fgets($fp, 1024), $header));
                $path = trim($header[1]);
                }
                fclose($fp);
                if ($break) break;
                }

                ?>

                Mind you, this code is provided "as is" and does not guarantee flawless
                behaviour under all circumstances!


                JW



                Comment

                • Bill

                  #9
                  Re: trouble using variable in fopen


                  "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
                  news:41b32057$0 $55226$abc4f4c3 @news.euronet.n l...
                  ....[color=blue]
                  >$host = 'www.livedepart ureboards.co.uk ';
                  >$port = 80;
                  >$path = '/ldb/train.aspx?T=FR NCMB++&J=137062 6&R=0';[/color]
                  ....[color=blue]
                  > Mind you, this code is provided "as is" and does not guarantee flawless
                  > behaviour under all circumstances![/color]

                  Thank you for your patient help.I am learning a lot from it and the code
                  does now work
                  However I am still getting the same problem when I try to use a variable
                  rather than hard code the value in $path.
                  Here is the code for a page you can try to see what I mean:
                  I hope my comments are clear.

                  <?php
                  $fp = fopen("http://www.livedepartu reboards.co.uk/ldb/summary.aspx?T= FNC",
                  'r');
                  if (stream_set_tim eout($fp, 5) == false){
                  echo "<p>&nbsp;</p><p><font color=\"red\">E rror: Connection Timed
                  Out</font></p>";
                  fclose($fp);
                  }elseif (!$fp){
                  echo "<p><font color=\"red\">E rror: Could Not Connect</font></p>";
                  fclose($fp);
                  }else{
                  $events = fgets($fp);
                  while ($events){
                  $portsmouth = strpos($events, 'Portsmouth');
                  if($portsmouth ){
                  $strBegins =strpos($events , 'train' );
                  $strEnds =strpos($events , '>Port' );
                  $strWanted = substr ( $events, $strBegins,$str Ends -
                  $strBegins -1);
                  $strTarget ="/ldb/".$strWante d;
                  }
                  $events = fgets($fp);
                  }
                  fclose($fp);
                  }

                  echo "strTarget is ".$strTarget."< br><br>";
                  echo " the url we want to go to is
                  www.livedepartu reboards.co.uk" .$strTarget." <br><br>";
                  echo " you can paste this into your browser to confirm<br>";
                  echo "If you promptly hard code the path variable with ".$strTarge t." you
                  will get the proper page:<br><br> ";
                  echo "However the following is the content of the page accessed by using the
                  strTarget variable in the path <br><br><br>" ;



                  $host = 'www.livedepart ureboards.co.uk ';
                  $port = 80;

                  //comment out the path you are not using
                  //$path = 'PASTE HERE';
                  $path = $strTarget;
                  $break = false;

                  while (true) {
                  $fp = fsockopen($host , $port);
                  fputs($fp, "GET $path HTTP/1.0\r\n");
                  fputs($fp, "Host: $host\r\n");
                  fputs($fp, "User-Agent: {$_SERVER['HTTP_USER_AGEN T']}\r\n");
                  fputs($fp, "\r\n");

                  // Get the response code
                  $response = (int) strstr(fgets($f p, 1024), " ");

                  if (($response != 301) && ($response != 302)) {
                  fpassthru($fp);
                  $break = true;
                  } else {
                  while (!preg_match("/^Location:(.+)$/i",
                  fgets($fp, 1024), $header));
                  $path = trim($header[1]);
                  }
                  fclose($fp);
                  if ($break) break;
                  }
                  ?>








                  Comment

                  • Janwillem Borleffs

                    #10
                    Re: trouble using variable in fopen

                    Bill wrote:[color=blue]
                    > Thank you for your patient help.I am learning a lot from it and the code
                    > does now work
                    > However I am still getting the same problem when I try to use a variable
                    > rather than hard code the value in $path.
                    > Here is the code for a page you can try to see what I mean:
                    > I hope my comments are clear.
                    >[/color]

                    I see what's happening. In order to be XHTML complient, the page you are
                    retrieving contains links like the following:

                    <td><a href="train.asp x?T=FRNCMB++&am p;J=1400907&amp ;R=0">London
                    Waterloo</a></td>

                    So, instead of '&', it uses the entity ('&amp;'). This is okay when
                    clicking the link in your browser, which will automatically convert the
                    entity to the implied character.

                    PHP doesn't have this mechanism, so you will have to tell it explicitly
                    to convert entities. One way of doing this is by calling str_replace, e.g.:

                    $path = str_replace("&a mp;", "&", $strTarget);


                    JW

                    Comment

                    • Bill

                      #11
                      Re: trouble using variable in fopen


                      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
                      news:41b48fc1$0 $8899$abc4f4c3@ flip.euronet.nl ...[color=blue]
                      > I see what's happening. In order to be XHTML complient, the page you are
                      > retrieving contains links like the following:
                      >
                      > <td><a href="train.asp x?T=FRNCMB++&am p;J=1400907&amp ;R=0">London
                      > Waterloo</a></td>
                      >
                      > So, instead of '&', it uses the entity ('&amp;'). This is okay when
                      > clicking the link in your browser, which will automatically convert the
                      > entity to the implied character.
                      >
                      > PHP doesn't have this mechanism, so you will have to tell it explicitly to
                      > convert entities. One way of doing this is by calling str_replace, e.g.:
                      >
                      > $path = str_replace("&a mp;", "&", $strTarget);[/color]

                      Thank you very much.it all works now.
                      You have taken the trouble of looking at the source code in the page and I
                      am very grateful.
                      I had noticed the presence of &amp there but had not realised its
                      significance.

                      When I first saw your final solution I did not think it would work because
                      of course when I echoed the string with
                      echo "strTarget is ".$strTarge t;
                      and got something like strTarget is
                      ldb/train.aspx?T=FR NCMB++&J=137062 6&R=0
                      I then rewrote the code by copying and pasting that into $path and it worked
                      while $path = $strTarget did not

                      I think I understand what is happening now.
                      It is not just in the address line that the browser converts the entity.
                      The string ldb/train.aspx?T=FR NCMB++&J=137062 6&R=0 is not what is in the
                      variable. It is what the browser has has converted the variable into.
                      The browser when it prints out the variable for me is replacing the &amp so
                      what I have been copying and pasting is not what is actually in the
                      variable.

                      So when I asked "Is there any way php treats a variable differently from a
                      hard coded string when used in a function? " I should have been asking "is
                      there any way the browser treats them differently?"

                      Please correct me if I am wrong. I really want to understand this


                      Comment

                      • Janwillem Borleffs

                        #12
                        Re: trouble using variable in fopen

                        Bill wrote:[color=blue]
                        > So when I asked "Is there any way php treats a variable differently from a
                        > hard coded string when used in a function? " I should have been asking "is
                        > there any way the browser treats them differently?"
                        >
                        > Please correct me if I am wrong. I really want to understand this
                        >
                        >[/color]

                        Browsers render entities, so indeed when you find '&amp;' in the source,
                        the browser displays an ampersand and when '&amp;amp;' is in the source,
                        the browser will display '&amp;'.

                        This makes looking at the source often a better way of debugging than
                        checking the output in a browser.


                        JW

                        Comment

                        • Bill

                          #13
                          Re: trouble using variable in fopen


                          "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
                          news:41b6e5b1$0 $19028$ee9da40f @snatched.euron et.nl...[color=blue]
                          > Browsers render entities, so indeed when you find '&amp;' in the source,
                          > the browser displays an ampersand and when '&amp;amp;' is in the source,
                          > the browser will display '&amp;'.
                          >
                          > This makes looking at the source often a better way of debugging than
                          > checking the output in a browser.[/color]

                          That is clear now.
                          Many thanks for your help.
                          Bill


                          Comment

                          Working...