473,320 Members | 1,694 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,320 software developers and data experts.

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:
http://www.livedepartureboards.co.uk...&J=1370626&R=0

If I hard code this string like this
$fp =
fopen("http://www.livedepartureboards.co.uk/ldb/train.aspx?T=FRNCMB++&J=1370626&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($strTarget, '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
Jul 17 '05 #1
12 3271
"Bill" <bi**@spamsucks.com> wrote in message
news:4L*******************@news.xtra.co.nz...
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:
http://www.livedepartureboards.co.uk...&J=1370626&R=0
If I hard code this string like this
$fp =
fopen("http://www.livedepartureboards.co.uk/ldb/train.aspx?T=FRNCMB++&J=1370
626&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
echo "[$strTarget]<br>\n";
$fp = fopen($strTarget, 'r');


Your parsing routine cannot be working as well as you are expecting it to
work.
Jul 17 '05 #2

"CJ Llewellyn" <in*****@example.con> wrote in message
news:co**********@slavica.ukpost.com...
"Bill" <bi**@spamsucks.com> wrote in message
news:4L*******************@news.xtra.co.nz...
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:

http://www.livedepartureboards.co.uk...&J=1370626&R=0

If I hard code this string like this
$fp =

fopen("http://www.livedepartureboards.co.uk/ldb/train.aspx?T=FRNCMB++&J=1370
626&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


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


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

It was echo $strTarget that gave me
http://www.livedepartureboards.co.uk...70626&R=0which I then used to hard code the function

Jul 17 '05 #3
Bill wrote:
http://www.livedepartureboards.co.uk...&J=1370626&R=0
which I then used to hard code the function


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

Jul 17 '05 #4

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41**********************@news.euronet.nl...
Bill wrote:
http://www.livedepartureboards.co.uk...&J=1370626&R=0
which I then used to hard code the function


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


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
programmatically 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?

Jul 17 '05 #5
Bill wrote:
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?


No, but the problem can be related to livedepartureboards.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 = 'ivedepartureboards.co.uk';
$post = 80;
$path = '/ldb/train.aspx?T=FRNCMB++&J=1370626&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_AGENT']}\r\n");
fputs($fp, "\r\n");

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

Jul 17 '05 #6
> 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 = 'ivedepartureboards.co.uk';
$post = 80;
$path = '/ldb/train.aspx?T=FRNCMB++&J=1370626&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_AGENT']}\r\n");
fputs($fp, "\r\n");

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


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

Warning: fsockopen() [function.fsockopen]: unable to connect to
livedepartureboards.co.uk:0 (Failed to parse address
"livedepartureboards.co.uk")

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

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses:
getaddrinfo failed: No address associated with hostname

In case it is relevant stream_get_transports() returns : Array ( [0] =>
tcp [1] => udp [2] => unix [3] => udg )
Jul 17 '05 #7
Bill wrote:
Thanks for this detailed help.
I am afraid I now have problems with fsockopen()
When I try the above with $host = 'livedepartureboards.co.uk'; I get

Warning: fsockopen() [function.fsockopen]: unable to connect to
livedepartureboards.co.uk:0 (Failed to parse address
"livedepartureboards.co.uk")


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

$host = 'www.livedepartureboards.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.livedepartureboards.co.uk';
$port = 80;
$path = '/ldb/train.aspx?T=FRNCMB++&J=1370626&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_AGENT']}\r\n");
fputs($fp, "\r\n");

// Get the response code
$response = (int) strstr(fgets($fp, 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

Jul 17 '05 #8

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41***********************@news.euronet.nl...
....
$host = 'www.livedepartureboards.co.uk';
$port = 80;
$path = '/ldb/train.aspx?T=FRNCMB++&J=1370626&R=0'; .... Mind you, this code is provided "as is" and does not guarantee flawless
behaviour under all circumstances!


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.livedepartureboards.co.uk/ldb/summary.aspx?T=FNC",
'r');
if (stream_set_timeout($fp, 5) == false){
echo "<p>&nbsp;</p><p><font color=\"red\">Error: Connection Timed
Out</font></p>";
fclose($fp);
}elseif (!$fp){
echo "<p><font color=\"red\">Error: 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,$strEnds -
$strBegins -1);
$strTarget ="/ldb/".$strWanted;
}
$events = fgets($fp);
}
fclose($fp);
}

echo "strTarget is ".$strTarget."<br><br>";
echo " the url we want to go to is
www.livedepartureboards.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 ".$strTarget." 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.livedepartureboards.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_AGENT']}\r\n");
fputs($fp, "\r\n");

// Get the response code
$response = (int) strstr(fgets($fp, 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;
}
?>


Jul 17 '05 #9
Bill wrote:
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.


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.aspx?T=FRNCMB++&amp;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("&amp;", "&", $strTarget);
JW

Jul 17 '05 #10

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41**********************@flip.euronet.nl...
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.aspx?T=FRNCMB++&amp;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("&amp;", "&", $strTarget);


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 ".$strTarget;
and got something like strTarget is
ldb/train.aspx?T=FRNCMB++&J=1370626&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=FRNCMB++&J=1370626&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
Jul 17 '05 #11
Bill wrote:
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


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

Jul 17 '05 #12

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41***********************@snatched.euronet.nl ...
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.


That is clear now.
Many thanks for your help.
Bill
Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
7
by: Michael J. Astrauskas | last post by:
I have a script, which I've called test-loadpic.php and some pages reference it by means of <img src="test-loadpic.php?sourcepic=$picNum"> where $picNum stores a number. This part itself works...
3
by: Oxygenearth | last post by:
Please who could help me with this... I had my structure in Win32, with Apache, PHP, and MySQL, I had a page in which I am transfering an image to the database in MySQL using PHP. But now I am...
3
by: James | last post by:
Hi guys, I have been building a search engine here - not because I have plans of dethrowning Google but as a simple app upon which to develop a function set that I can use for other things. ...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
6
by: finer recliner | last post by:
i've been playing with the below code for a while now, and i cant get it to work correctly. the user should be able to input text into a textbox and upon hitting submit, it will append that text to...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.