PHP脚本自行刷新并重新启动执行时间 [英] PHP-script refresh it self and restart execution-time

查看:142
本文介绍了PHP脚本自行刷新并重新启动执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含约500多个元素的数组.这些元素将在函数中进行检查,然后我将不得不从API中为每个元素(每个元素都是一个查询)获取数据,这不允许我在短时间内收到太多请求.我必须运行一个延迟循环,很可能会超过30秒.

I have an array with about 500+ elements. These elements will be checked in a function and then I will have to grab data from an API for each element (every element is one query), that does not allow me that much requests in a short time. I will have, to run a delayed loop, that will very likely exceed 30 secs.

我想要的是,我的PHP脚本应该进行一定数量的检查/请求,并从待办事项"列表中删除,然后自我刷新并在大约2秒钟后继续工作.

What I want is, that my PHP-script should do a certain amount of checks/requests and remove from the "todo"-list and then self refresh and continue the jobafter ~2 sec.

cronjob将启动此php脚本.

A cronjob will start this php-script.

在工作完成"之后或发生某种故障"之后,如何管理PHP重新启动脚本?这取决于这件事,取决于我如何将"todo-list"数组中的数据存储到文件或$ _SESSION中.不想将其存储到数据库中.

How can I manage PHP to restart a script after "work is done" or after some kind of "failure" occurs? It depends on this thing, on how I store the data from the "todo-list"-array into either a file, or a $_SESSION. Don't want to store this into a DB.

我如何解决此问题而无需在服务器上或脚本本身之外进行设置?

How can I solve this without the need to setup something on the server, or outside of the script itself?

推荐答案

PHP生命周期是基于请求的,即它本身无法刷新.

PHP life cycle is a request based, i.e. it can't be refreshed by itself.

您可以使用:

  1. cronjob在后台做一些工作
  2. 计时器上的Java脚本,用于从浏览器向Web服务器启动新请求,该请求将执行PHP API并返回新结果.例如:

<script>
function onTimerRefreshFromApi() {
    var request = new XMLHttpRequest();
    request.open('GET', '/api/url', true);

    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            // On Success replace html
            element.innerHTML = this.response;
        } else {
            // We reached our target server, but it returned an error
        }
    };

    request.onerror = function () {
        // There was a connection error of some sort
    };

    request.send();
}

setInterval(onTimerRefreshFromApi, 1000);
</script>

  1. 在计时器上使用Ajax的方式与2)类似.
  2. 使用标头指示浏览器按照以下说明进行刷新:使用PHP刷新页面

这篇关于PHP脚本自行刷新并重新启动执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆