小编Noé*_*Noé的帖子

发送HTTP响应后如何继续PHP脚本

我想知道是否可以立即发送 HTTP 响应并继续执行脚本。

背景:在我向服务器提出的所有请求中,有一个创建了 Excel 文件(使用 PHPSpreadSheet),因为创建此文件可能需要更长的时间,我正在考虑向客户端响应HTTP 202 状态代码,例如:

header("HTTP/1.1 202 Excel file in process");

并在每次状态代码到达时在主 js 文件上编写一个侦听器,以激活时间间隔 ( setInterval()) 并每隔一定秒数询问 Excel 文件是否准备就绪

$(document).ajaxSuccess(function ( event, xhr, settings) {
    if(xhr.status === 202) {
        //Activate the setInterval function to ask the server every 10 seconds whether the file is ready or not
    }
});

Run Code Online (Sandbox Code Playgroud)

(我知道我必须激活/停用间隔)

因此,每当我收到有关 createExcel.php 文件的请愿书时,我想立即回复用户已收到请愿书并开始制作此类 Excel 文件

就像是:

<?php
//Tell user that the petition has being received 
header("HTTP/1.1 202 Excel file in process");
//Kill this …
Run Code Online (Sandbox Code Playgroud)

javascript php

6
推荐指数
1
解决办法
163
查看次数

标签 统计

javascript ×1

php ×1