发送响应并继续执行脚本 - PHP

时间:2017-10-12 11:46:34

标签: php asynchronous

我想知道在完成执行脚本之前是否可以向reqeust发送响应?

例如,有服务器A向我的服务器B发送请求(server2server请求)。除了向服务器A发送响应之外,我还需要对其他api执行一些curl请求并将一些数据保存到DB。 我不想强制服务器A等待这些额外的任务,因为它可以超时连接。

使用队列(如Beanstalkd)有一种解决方法,但我想知道是否有可能以我描述的方式进行,以及是否存在使用此方法的危险。

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。

ignore_user_abort(true);
set_time_limit(0);

ob_start();
// do initial processing here
echo $response; // send the response
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();

// now the request is sent to the browser, but the script is still running
// so, you can continue...
相关问题