davefootball123 Posted February 3, 2013 Share Posted February 3, 2013 (edited) I coded a PHP script that sends a text message to a list of people who are subscribed when severe weather warnings are issued. The script works good. However the problem right now is if I have it on a cron job every 5 minutes to check for a warning, every 5 minutes that a warning is in effect a text message will be sent to the subscribers. Is there a way to have a 5 minutes cron job but only send the text if the external file at the weather service is updated? That probably sounds really complicated but I will include my simple part of the text message script. Thanks for the help, Dave <?PHP //SUBJECT AND BODY OF EMAIL SHOULD BE LESS THAN 160 CHARACTERS TOTAL $url = 'http://www.weatheroffice.gc.ca/rss/warning/on-151_e.xml'; $search_torwarn = 'SNOW SQUALL WARNING IN EFFECT'; $subject = "SOWX ALERT"; $message = "Snow Squall Warning For: Barrie - Collingwood - Hillsdale. Visit http://www.sowx.ca for more information. "; $file_contents = file_get_contents($url); if(strpos($file_contents, $search_torwarn) !== FALSE){ mail("@msg.telus.com,6@msg.telus.com",$subject,$message,"From: SOWX ALERTS"); } ?> Edited February 3, 2013 by davefootball123 Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/ Share on other sites More sharing options...
requinix Posted February 3, 2013 Share Posted February 3, 2013 Store the time of the most recent alert somewhere, like a database, then only send the message if there's anything newer. Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1409813 Share on other sites More sharing options...
davefootball123 Posted February 3, 2013 Author Share Posted February 3, 2013 Would looking at the time of the bulletin work? And how would you go about comparing the time to the bulletin? Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1409814 Share on other sites More sharing options...
davefootball123 Posted February 3, 2013 Author Share Posted February 3, 2013 OK I noticed something http://www.weatheroffice.gc.ca/rss/warning/on-151_e.xml the xml has a publish date/time. Could you compare that to current time and say if time is < 2 minutes then send a text. Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1409815 Share on other sites More sharing options...
requinix Posted February 3, 2013 Share Posted February 3, 2013 >5 minutes ago, yes. It's not as foolproof as storing the time from the last alert but it'll probably be good enough. Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1409824 Share on other sites More sharing options...
davefootball123 Posted February 3, 2013 Author Share Posted February 3, 2013 Could the time of the last alert be stored in a text file? Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1409825 Share on other sites More sharing options...
trq Posted February 3, 2013 Share Posted February 3, 2013 If you have sufficient access to install software on this system you could use incron instead of a regular cron daemon. incron runs tasks based on filesystem events. This allows you to execute a script when a file changes for instance. Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1409830 Share on other sites More sharing options...
davefootball123 Posted February 3, 2013 Author Share Posted February 3, 2013 I think what im going to do is read the xml issuance time. I will have a script read the xml for issuance of the warning. And if its new it will trigger the text message script. Do you think that would work? Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1409936 Share on other sites More sharing options...
davefootball123 Posted February 4, 2013 Author Share Posted February 4, 2013 I managed to get the creation time/update time of the bulletin and but it in a text file. That works good. Considering I only need it for the one file the next step may be easy. How can I have the script look at the text file with the time of the bulletin and have it only run the text script if the bulletin is new? Any Ideas or help would be greatly appreciated. Thanks, Dave Quote Link to comment https://forums.phpfreaks.com/topic/273969-run-php-file-when-external-file-is-updated/#findComment-1410097 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.