I am developing an api which processes user request and sends them response in JSON format. I want to send JSON response and then do the work which takes more time e.g. sending mail to the user. Script pseudo code
- do validations
- if its valid then update database
- if d/b update successful then send JSON response
- get out of ifs and else of validations
- send email to the user
I referred this stackoverflow solution which runs exactly as I wanted in browser but when I test it in POSTMAN or in device where this API is being used I don't get the exact response. Browser responds quickly while loading the entire page but postman doesn't display result till the end of execution, e.g. code
<?php
ignore_user_abort(true);
set_time_limit(0);
ob_start();
// do initial processing here
echo json_encode(array("date", strtotime(date('Y-m-d H:i:s')))) . "<br />"; // send the response
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();
for ($i = 0; $i < 999999999; $i++) {
}
echo json_encode(array("date", strtotime(date('Y-m-d H:i:s'))));
?>
above code works fine in browser but when called in POSTMAN REst client it executes for longer time Thanks in advance