|
Posted by Jason on 04/05/05 12:07
Could someone tell me what I did wrong with this script. It should read
the page
http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm and
only read between the 2 words in the script. And email any new stuff to the
email address in the script. What did I do wrong that is causing this
script to fail?
<?
# ----------------------------------------------------------------------------------
# Configuration
# Directory on the remote server. Be sure to include leading and trailing
slashes!
$remotedir = "/english/traveller/conditions/";
# File on the remote server. No slashes!
$remotefile = "rdclosure.htm";
# Keyword to check for in response.
$keyword = "Closed";
# Remote server address.
$remoteserver = "www.mto.gov.on.ca";
# E-mail recipient(s). Separate each address with a comma and a space.
$emailrecip = "webmaster@weatherserver.net";
# E-mail subject line.
$emailsubject = "MTO - TEST" . date(' (G:i:s)');
# E-mail From name.
$emailfromname = "WxServer Roads";
# E-mail From address.
$emailfromaddr = "WxServer@weatherserver.net";
# End Configuration
# ----------------------------------------------------------------------------------
# Format the page for easy viewing.
Header( "Content-type: text/html");
# Setup the request.
$header .= "GET " . $remotedir . $remotefile . " HTTP/1.0\r\n";
$header .= "Host: " . $remoteserver . "\r\n";
$header .= "User-Agent: Downloader\r\n\r\n";
$fp = fsockopen ($remoteserver, 80, $errno, $errstr, 30);
# Do the request.
fputs ($fp, $header . $req) or die("Can't access the site!");
while (!feof($fp)) {
$res .= fgets ($fp, 128);
}
# Strip off the header info.
$res = preg_replace("/<[^>]+>/", "", $res);
$res = preg_replace("/Last Updated\:\s+?\d\d\d\d-\d\d-\d\d\s+?\d\d\:\d\d/",
"", $res);
$res = preg_replace("/ /", "", $res);
$headerend = strpos($res,"\r\n\r\n");
if (is_bool($res)) {
$result = $res;
}
else {
$result = substr($res,$headerend+4,strlen($res) - ($headerend+4));
}
fclose ($fp);
# Start and end tags
$startstr = "Highways";
$endstr = "This";
# Find start and end positions
$startpos = strpos($result, $startstr) + strlen($startstr);
$endpos = strpos($result, $endstr, $startpos);
# Get string between 2 tags
$result = substr($result, $startpos, $endpos-$startpos);
echo ($new_res);
# Check for keyword.
if (!stristr($result, $keyword)) die("ERROR Code Word Not Found");
# Read the file containing the last response.
$filename = $remotefile . '.txt';
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);
# Check for changes.
if ($contents == $new_res) {
# There has not been a change.
echo ("No Updates\r\n");
} else {
# There has been a change.
echo ("**UPDATES DETECTED**\r\n");
# Write the new file.
$filename = $remotefile . '.txt';
$fp = fopen($filename, "w");
$write = fputs($fp, $result);
fclose($fp);
# Send the e-mail.
$recipient = $emailrecip;
$subject = $emailsubject;
$body_of_email = $result;
$header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>';
mail ($recipient, $subject, $body_of_email, $header);
echo ("E-mail sent.");
}
?>
[Back to original message]
|