简体   繁体   English

PHP每X分钟执行一次操作

[英]PHP perform action every X minutes

I've made a PHP script that gets tweets from twitter and saves them to a file for use (as JSON). 我已经制作了一个PHP脚本,该脚本从twitter获得推文,并将其保存到文件中以供使用(作为JSON)。 Problem is that I only get the tweets once when I execute the script, but I want to get it about every 4-5 minutes. 问题是我在执行脚本时只收到一次推文,但我希望每4-5分钟获得一次。

I've looked into sleep but that just delays the browser and executes the action once. 我已经进入sleep但这只会延迟浏览器并执行一次操作。 I want to be executed every 4 minutes, not just once after a delay. 我希望 4分钟执行一次 ,而不仅仅是延迟执行一次

I looked into cron aswell but the server that I'm going to run my application on does not allow me to use cron. 我也调查了cron,但是要在其上运行应用程序的服务器不允许我使用cron。

TL;DR: Anybody know a good way to execute a php script every X minutes after the first execution? TL; DR:有人知道第一次执行后每隔X分钟执行php脚本的好方法吗?

The best bet for this would be to schedule a task to call your PHP page every x mins rather than trying to keep a thread open. 最好的选择是安排一个任务每x分钟调用一次PHP页面,而不是尝试保持线程打开。 Take a look at a CRON JOB . 看一下CRON JOB

The scheduled task doesn't need to be executed from the server if you make the page publically accessible. 如果您可以公开访问页面,则不需要从服务器执行计划任务。

您可以设置一个Web监视服务(某些服务是免费的,例如Pingdom)以每隔X分钟向您的页面发出HTTP请求,这可以解决问题。

There are providers that offer nothing but running cron jobs for you. 有些提供程序只为您提供cron作业。 Look into them, they could just call your php script every x minutes. 调查他们,他们可以每x分钟调用您的php脚本。

Everything you do to make this work without cron is a hack. 要使这项工作在没有cron的情况下所做的一切都是骇客。

You could go 可以

while (1) {
  process(...)
  sleep(4*60)
}

But you'll run into problems with PHPs execution time limits. 但是您会遇到PHP执行时间限制的问题。

Maybe it's an option to have your script do the action once, and have it called from a cron-capable host every 4 minutes? 也许让脚本执行一次操作,然后每隔4分钟从具有cron功能的主机上调用一次脚本,是一种选择?

I've managed to solve the question myself. 我已经设法解决了自己的问题。 As stated in my question, Javascript and Jquery are also viable solutions. 如我的问题所述,Javascript和Jquery也是可行的解决方案。 I used a setInterval to perform a $.get on my twitter.php file every X minutes. 我每隔X分钟使用setInterval对我的twitter.php文件执行$ .get。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM