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
I spent a few hours on this issue and I have come with this function which works on Apache and Nginx:
You can call this function before your long processing.
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: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.Modified the answer by @vcampitelli a bit. Don't think you need the
close
header. I was seeing duplicate close headers in Chrome.I use the php function register_shutdown_function for this.
http://php.net/manual/en/function.register-shutdown-function.php
Edit: The above is not working. It seems I was misled by some old documentation. The behaviour of register_shutdown_function has changed since PHP 4.1 link link
If you're using FastCGI processing or PHP-FPM, you can:
Source: http://fi2.php.net/manual/en/function.fastcgi-finish-request.php