当php运行在FastCGI模式时,PHP FPM提供了一个名为fastcgi_finish_request()的函数。

比如我们在处理大文件导出的时候,可能就会超时,导致页面响应失败,报502的错误。

例子1:

echo 'hello';
fastcgi_finish_request();
echo 'world';

结果只输出到hello就结束了。

echo 'hello';
fastcgi_finish_request();
file_put_contents('./test.txt', 'world');

响应还是到hello,但是world会输出到文件。

这个函数相当于简化版到异步到效果了,比如发送邮件等不需要及时反馈等功能,先告诉用户成功了,然后去发送邮件,提高响应体验。

考虑代码兼容性,可能需要这样

if (!function_exists("fastcgi_finish_request")) 
{
			function fastcgi_finish_request()  
			{
					//do some thing
			}
}