Home  >  Q&A  >  body text

Continue processing php after sending http response

<p>My script is called by the server. I will receive <code>ID_OF_MESSAGE</code> and <code>TEXT_OF_MESSAGE</code> from the server. </p> <p>In my script, I will process the incoming text and generate a response using the parameters: <code>ANSWER_TO_ID</code> and <code>RESPONSE_MESSAGE</code>. </p> <p>The problem is that I'm sending a response to the incoming <code>"ID_OF_MESSAGE"</code>, but the server that sends me the message for processing sets its message to delivered to me (which means So I can send his response to that ID) after receiving http response 200. </p> <p>One solution is to save the message to a database and create some cron that runs every minute, but I need the response message to be generated immediately. </p> <p>Is there some solution how to send to server http response 200 and continue executing php script? </p> <p>Thank you very much</p>
P粉029327711P粉029327711441 days ago607

reply all(1)I'll reply

  • P粉351138462

    P粉3511384622023-08-28 17:34:11

    Yes. You can do this:

    ignore_user_abort(true);//not required
    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();
    fastcgi_finish_request();//required for PHP-FPM (PHP > 5.3.3)
    
    // now the request is sent to the browser, but the script is still running
    // so, you can continue...
    
    die(); //a must especially if set_time_limit=0 is used and the task ends
    

    reply
    0
  • Cancelreply