这个代码示例应该重定向并继续处理一个长操作,但是它直到exec
命令完成后才重定向。我已经尝试了多种其他方法,但都没有效果。我哪里做错了?
ignore_user_abort()在我的php.ini
<?php
set_time_limit ( 0 );
header ( 'Connection: close' );
ob_start ();
header ( 'Content-Length: 0' );
header ( 'Location: /redirect.php' );
ob_end_flush ();
flush ();
ignore_user_abort ( true );
exec('command that takes 5 minutes to process');
exit ();
?>
我提前感谢你的帮助
我最近必须编写一个webhook处理程序,它应该在继续处理其他长时间运行的任务之前立即返回" 200ok "响应。
我观察到,当我使用相同的web服务器进行整个过程时,立即挂断电话的过程是不可能的。因此,如果您需要发送响应的连接的客户端IP与服务器IP相同,您可能永远无法做到这一点。但是当传入的连接是一个真实世界的远程客户端时,它总是工作的。
说了这么多,我只是返回一个"200 OK"的响应。这应该与发送重定向没有什么不同。
为我工作的代码是…
public function respondASAP($responseCode = 200)
{
// check if fastcgi_finish_request is callable
if (is_callable('fastcgi_finish_request')) {
/*
* This works in Nginx but the next approach not
*/
session_write_close();
fastcgi_finish_request();
return;
}
ignore_user_abort(true);
ob_start();
http_response_code($responseCode);
header('Content-Encoding: none');
header('Content-Length: '.ob_get_length());
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
}
你可以根据你的情况调整它,让它设置一个重定向头,而不是一个"200 OK"