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.
我正在设置一个CRON来检测页面上的更改。我遇到的问题是更多地指向服务器端,但我希望其他人看一下代码,看看是否有任何明显的问题会阻碍CRON正常运行。 CRON运行脚本并在某些时候单独工作,结果要么100%正确,要么电子邮件被延迟,并且消息中的$ nws_timestamp错误的时间戳。
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";
?>
1 个解决方案
#1
Solved;
I ended up running a few more tests and found that the script was running fine, and there was not problem with the location of the script being run and calling files.
我最终运行了一些测试,发现脚本运行正常,并且运行和调用文件的位置没有问题。
The problem resided in the mail() function being used in the cron, I have moved over to PHPMailer to send the mail in the script and logging in to SMTP to send the mail. Works every time on time.
问题在于cron中使用的mail()函数,我已经转移到PHPMailer以在脚本中发送邮件并登录到SMTP以发送邮件。每次按时工作。
Thanks for the help,
谢谢您的帮助,
#1
Solved;
I ended up running a few more tests and found that the script was running fine, and there was not problem with the location of the script being run and calling files.
我最终运行了一些测试,发现脚本运行正常,并且运行和调用文件的位置没有问题。
The problem resided in the mail() function being used in the cron, I have moved over to PHPMailer to send the mail in the script and logging in to SMTP to send the mail. Works every time on time.
问题在于cron中使用的mail()函数,我已经转移到PHPMailer以在脚本中发送邮件并登录到SMTP以发送邮件。每次按时工作。
Thanks for the help,
谢谢您的帮助,