473,663 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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
Jul 17 '05 #1
12 3302
"Bill" <bi**@spamsucks .com> wrote in message
news:4L******** ***********@new s.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.livedepartu reboards.co.uk/ldb/train.aspx?T=FR NCMB++&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($strTarge t, 'r');


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

"CJ Llewellyn" <in*****@exampl e.con> wrote in message
news:co******** **@slavica.ukpo st.com...
"Bill" <bi**@spamsucks .com> wrote in message
news:4L******** ***********@new s.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.livedepartu reboards.co.uk/ldb/train.aspx?T=FR NCMB++&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($strTarge t, '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.c om> 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
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?

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

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 = '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


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 )
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 = '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")


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

Jul 17 '05 #8

"Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
news:41******** *************** @news.euronet.n l...
....
$host = 'www.livedepart ureboards.co.uk ';
$port = 80;
$path = '/ldb/train.aspx?T=FR NCMB++&J=137062 6&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.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;
}
?>


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.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

Jul 17 '05 #10

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

Similar topics

9
4950
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
7
2368
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 fine. test-loadpic.php uses the "sourcepic" GET variable to reference a file on disk, get it's file/MIME type, transmit the headers and then the raw image. The important code is below:
3
3639
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 in Apache/Linux/MySQL(FreeBSD) with the same files. My problem is.. when I try to submit the variables to the php file, this does not get the binFile, in other words, the file($_POST) does not pass throug the SUBMIT html statemenT, so it is not...
3
4554
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. So I coded my little search engine, uploaded it and it didn't work with my host (Dreamhost.com): Warning: fopen(): URL file-access is disabled in the server
4
5629
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 results in a file, which doesn't hold the information of the changed array anymore. I dont know why. Here is the code: /*
0
1710
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 I want the latest comment to be on top, and that's where I'm running into trouble. Since I know very little about PHP, I thought I was clever in what I came up with. I think it can work if I get the coding right. Let me know if my logic is wrong. I'm...
6
1390
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 whatever is in "newpage.html" and write this new chunk of text to data.txt. the final result of my code will be a bit more elaborate, but i can't even get this to work. i believe soemthing is wrong the scope of my variables or how my functions...
4
3263
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 variables I'd like to display a state. So I wanted to use maps for quick look-ups... anyway here's my code for filling and verifying the contents of my map of maps: include<map> #define MAX_LENGTH 80; void gen_enums(void) { char varname =...
20
5101
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 snip in question: $file = "time";
0
8436
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.