I am setting up a CRON to detect a change on a page. The issue I am having is pointing more to server side but I would like some others to look at the code and see if any obvious problems stick out that would hinder the CRON from running right. The CRON runs the script and works some of the time on its own, the results are either 100% correct or the email is delayed with the wrong time stamp coming from $nws_timestamp in the message.
When the script is run via the browser is works every time correctly.
Summery of the script; reads a text file with the remote data saved on it and compares the two, pulls and trims just the date and time of update. If different it sends an email to a few emails with the updated date and time.
Purpose of script: Allows a list of people to be notified when the National Weather Service updates the weather briefings during storm events. (like the current tropical storm on us now)
<?
$source = file_get_contents('http://www.erh.noaa.gov/mhx/downloads/briefings/index.php');
$textfile = 'nws_brief.txt';
list($junk,$nws_timestamp) = explode("Updated: </td><td align=\"left\">",$source);
$nws_timestamp = substr("$nws_timestamp", 0, 26); //location of text on page
$nws_timestamp = str_replace("</td>", "", "$nws_timestamp");
$nws_timestamp = str_replace("</tr>", "", "$nws_timestamp");
$nws_timestamp = str_replace("<", "", "$nws_timestamp");
$nws_timestamp = str_replace("/", "", "$nws_timestamp");
$nws_timestamp = str_replace("/t", "", "$nws_timestamp");
$nws_timestamp = str_replace("td", "", "$nws_timestamp");
$nws_timestamp = str_replace("d>", "", "$nws_timestamp");
$nws_timestamp = str_replace(">", "", "$nws_timestamp");
$textfile_data = file_get_contents($textfile);
//READ FROM FILE
if ($textfile_data == $nws_timestamp){
exit;
}else{
//Continue
}
//SAVE TO FILE
$current = file_get_contents($textfile);
$current = "$nws_timestamp";
file_put_contents($textfile, $current);
//EMAIL NOTIFICATION
//BCC List:
$bcc_list = array(
"user2@domain.com",
"user3@domain.com",
"user4@domain.com",
"user5@domain.com",
);
$bcc = implode(',', $bcc_list);
putenv('TZ=America/New_York');
$date_time = date('m-d-Y g:i:s A');
$to = "user1@domain.com";
$headers = "From: OBXAirWaves <admin@obxairwaves.com>\r\n";
$headers .= "Organization: OBXAirWaves.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "BCC: $bcc\r\n";
$subject = "NWS Briefing Update - $nws_timestamp";
$msg = "NWS Update - $date_time<br>
<a href=\"http://www.erh.noaa.gov/mhx/downloads/briefings/index.php \">Download Latest Briefing</a><br>
<br>
Briefing Update:<br>
<table border='1'>
<tr>
<td>New: $nws_timestamp</td>
</tr>
<tr>
<td>Last: $textfile_data</td>
</tr>
</table>
";
mail($to, $subject, $msg, $headers);
echo "Date & Time: $date_time<br>To: $to<br>BCC: $bcc<br>Subject: $subject<br><br>Message: <br>$msg";
?>