"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> </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;
}
?>