如何在 PHP 中触发未来某个时间的事件
我编写了一个 PHP 应用程序,用于向数据库中的电话号码广播语音消息。
现在我想为其添加“时间表”功能。 这基本上意味着管理员能够为特定语音文件设置未来的日期和时间。语音消息将在该日期和时间播出。
请问我该如何编码? 一些代码片段将受到高度赞赏。
谢谢, 阿米特
I have written a PHP application that broadcasts a voice message to phone numbers in the database.
Now I want to add a "Schedule" functionality to it.
It basically means that the administrator would be able to set a future date and time for a particular voice file. The voice messages will be broadcast at exactly that date and time.
How can I code this please?
Some code snippets will be highly appreciated.
Thanks,
Amit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要研究 CRON 作业来自动执行脚本。看一下: http://net.tutsplus .com/tutorials/php/managing-cron-jobs-with-php-2/ 了解更多信息。
You need to look into CRON jobs to automate script execution automatically. Take a look at: http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/ for some more info.
正如 Tom Walters 所说,cron(或者计划任务,如果您使用的是 Windows)可能是一个好方法(至少在一开始是这样)。
Cron 的最大分辨率是 1 分钟,所以希望这足够精确。
考虑一个如下表:
然后编写一个执行以下操作的脚本:
时间,其中实际时间为 NULL。 (类似
SELECT * FROM
)调用 WHEREual_time IS NULL and target_time <= NOW()
设置实际时间。
然后,您使用 cron (或其他)每 X 分钟运行该脚本。
这是一个可以帮助您继续前进的基本架构。
当然,如果您有多条线路用于出站呼叫或其他奇特的要求,事情会变得更加复杂。
如果您要求精确度接近秒,那么 cron 将无法满足要求。此时,您可能会考虑编写一个或多个守护程序(连续运行的脚本)来更频繁地轮询数据库。
As Tom Walters says, cron (or Scheduled Tasks if you're using windows) is probably a good way to go (at least at first).
Cron's maximum resolution is 1 minute, so hopefully, that's precise enough.
Consider a table like:
Then you write a script that does the following:
time, where actual_time is NULL. (Something like
SELECT * FROM
)calls WHERE actual_time IS NULL and target_time <= NOW()
set actual_time as it goes.
Then, you use cron (or whatever) to run that script every X minutes.
That's a basic architecture that should get you going.
Of course, things get more complicated if you have multiple lines for outbound calls, or other fancy requirements.
If you require accuracy approaching seconds, cron is going to fall short. At that point, you might consider writing one or more daemons (scripts that run continuously) to poll the database more frequently.