continue processing php after sending http response

continue processing php after sending http response

laravel execute after response
php get http response code
php keep running after disconnect
request and response in php
php response header
php flush
php asynchronous processing

My script is called by server. From server I'll receive ID_OF_MESSAGE and TEXT_OF_MESSAGE.

In my script I'll handle incoming text and generate response with params: ANSWER_TO_ID and RESPONSE_MESSAGE.

The problem is that I'm sending response to incomming "ID_OF_MESSAGE", but server which send me message to handle will set his message as delivered to me (It means I can send him response to that ID), after receiving http response 200.

One of solution is to save message to database and make some cron which will be running each minute, but I need to generate response message immediately.

Is there some solution how to send to server http response 200 and than continue executing php script?

Thank you a lot

Yes. You can do this:


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

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

Continue processing PHP after sending 200 OK response, Keep the execution after sending HTTP response. In order to handle these situations it's recommended to keep replying to notifications  Therefore, I need PHP to send the complete HTTP response to the client as fast as possible (ideally, as soon as the input parameters have been validated), and then run the database and e-mail processing. I’m running mod_php, so pcntl_fork is not available.

I've seen a lot of responses on here that suggest using ignore_user_abort(true); but this code is not necessary. All this does is ensure your script continues executing before a response is sent in the event that the user aborts (by closing their browser or pressing escape to stop the request). But that's not what you're asking. You're asking to continue execution AFTER a response is sent. All you need is the following:

    // Buffer all upcoming output...

    // Send your response.
    echo "Here be response";

    // Get the size of the output.
    $size = ob_get_length();

    // Disable compression (in case content length is compressed).
    header("Content-Encoding: none");

    // Set the content length of the response.
    header("Content-Length: {$size}");

    // Close the connection.
    header("Connection: close");

    // Flush all output.

    // Close current session (if it exists).
    if(session_id()) session_write_close();

    // Start your background work here.

If you're concerned that your background work will take longer than PHP's default script execution time limit, then stick set_time_limit(0); at the top.

PHP: Send response and continue processing · GitHub, PHP: Send response and continue processing. respond_and_process.php. <?​php. // Buffer all upcoming output ob_start();. // Send your response. Continue processing PHP after sending 200 OK response. Recently I’ve come across APIs and mainly event management systems that need to receive a HTTP 200 OK response upon sending a request to our server. For example, if you need to integrate Slack Events API your server has to respond a HTTP 200 OK within 3 seconds to every event it receives.

If you're using FastCGI processing or PHP-FPM, you can:

session_write_close(); //close the session
fastcgi_finish_request(); //this returns 200 to the user, and processing continues

// do desired processing ...
$expensiveCalulation = 1+1;


continue processing php after sending http response, ignore_user_abort(true); set_time_limit(0); ob_start(); // do initial processing here echo $response; // send the response header('Connection:  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I spent a few hours on this issue and I have come with this function which works on Apache and Nginx:

 * respondOK.
protected function respondOK()
    // check if fastcgi_finish_request is callable
    if (is_callable('fastcgi_finish_request')) {
         * This works in Nginx but the next approach not



    $serverProtocole = filter_input(INPUT_SERVER, 'SERVER_PROTOCOL', FILTER_SANITIZE_STRING);
    header($serverProtocole.' 200 OK');
    header('Content-Encoding: none');
    header('Content-Length: '.ob_get_length());
    header('Connection: close');


You can call this function before your long processing.

In PHP, how would one send all output to client mid-script and end , Normally PHP sends the response after it has finished processing. sample I found at Continue PHP execution after sending HTTP response  PHP: Send response and continue processing. GitHub Gist: instantly share code, notes, and snippets. Clone via HTTPS Clone with Git or checkout with SVN using the

Modified the answer by @vcampitelli a bit. Don't think you need the close header. I was seeing duplicate close headers in Chrome.



echo '{}';
header($_SERVER["SERVER_PROTOCOL"] . " 202 Accepted");
header("Status: 202 Accepted");
header("Content-Type: application/json");
header('Content-Length: '.ob_get_length());


Send response to client in PHP and continue processing, Here's one way to send and close the connection to the client and for the PHP script to continue processing, presumably to perform some  This means that at the moment, only the PHP FPM server API is able to send a response to the client while the server's PHP process still performs some tasks. With all other server APIs, listeners to kernel.terminate are still executed, but the response is not sent to the client until they are all completed.

PHP: close browser connection and keep on executing, Sometimes you need to execute a really long process after displaying a web page. The key to doing this are the two HTTP header fields: send headers to tell the browser to close the connection How can I send a response back to the 1st, calling php file then continue with the rest of the code and  This code indicates that the server has received and is processing the request, but no response is available yet. 103 Early Hints. This status code is primarily intended to be used with the Link header, letting the user agent start preloading resources while the server prepares a response.

How to continue script execution in background in PHP? · Maslosoft, There are cases where we want to continue PHP script execution in background. fastcgi_finish_request(); // Send HTTP headers header('Content-Length: 0'); 0) { ob_end_clean(); ob_flush(); } flush(); // Background processing code  HTTP 102 Processing (WebDAV) RFC 2518 suggests using HTTP 102 Processing: The 102 (Processing) status code is an interim response used to inform the client that the server has accepted the complete request, but has not yet completed it. but it has a caveat: The server MUST send a final response after the request has been completed.

Can I finish request and then continue processing?, I want to return a response immediately to the user and then continue to work work after the HTTP response has already been sent to the browser. kernel.​php) and whilst the response is immediate, the browser knows that  Using http_response_code will cause PHP to match and apply a Reason Phrase from a list of Reason Phrases that are hard-coded into the PHP source code. 2. Because of point 1 above, if you use http_response_code you must set a code that PHP knows about.