0

I'm wondering if is it possibile to send reponse to the reqeust before finishing executing script?

For example, there is server A that is sending request to my server B (server2server request). Beside sending response to server A, I need to do some curl reques to other apis and save some data to DB. I don't want to force server A to wait for this additional tasks, because it can timeout connection.

There is a workaround using queues (like Beanstalkd) but I'd like to know if it is possible in a way I described and if there are any dangers using this approach.

Ziom Ziomalski
  • 115
  • 2
  • 12

1 Answers1

0

You can try something like this.

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...