Keep Coding

Never Stop Learning

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. Another example is where your server has to respond an [accepted] response within 10 seconds to Adyen notifications.

Keep the execution after sending HTTP response

In order to handle these situations it’s recommended to keep replying to notifications separated from processing them. Another solution is to first return a successful response and then continue execution of the script in the same process. To achieve this I’ve written the following function:

So assuming you’ve got a function named handleEvent() which is called whenever your server receives a request you can use respondOK() function as follow:

You can find more around this topic here and here. (Edit: I’m the author of the answer on SO, I thought people can relate the domain with the answer author, but apparently not!)


  1. Any idea why it gets stuck when trying to fetch an API after sending resposneOK()?

    Try something like
    $firstHit = file_get_contents($entrypoint);
    error_log('before response: ' . $firstHit);

    error_log('slept for 1 sec');

    $secondHit = file_get_contents($entrypoint);
    error_log('Second hit: ' . $secondHit);

  2. Hi thanks for your post. I’ve got a situation where I need to send multiple emails to a rabbitmq queue looping through the emails from database using foreach this is causing a long waiting time for the user to wait for response. Do you think I can use this function?

  3. Abdoulaye Siby

    22 May 2018 at 7:32 am

    Hi there. Great snippet. Thanks.

    Any reason why I can get it to work on MAMP?

    I had to use it on a VPS running Ubuntu and Apache/PHP to get it to work.

  4. Abdoulaye Siby

    22 May 2018 at 7:52 am

    Instead of using the following technique …

    Wouldn’t it be more portal to use the http_response_code() PHP function?

  5. I implemented your solution on PHP running on Windows Server 2012 R2 (IIS 6.2).

    Sadly it does not work. I call $this->respondOK(); early on the handleEvent function (1 sec after it start) , but the calling system (that is the “Slack” in my architecture) is still waiting until my script is finished (60 seconds later).

    Any ideas?

  6. Maybe credit the stackoverflow answer next time you blatantly copy code?

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2022 Keep Coding

Theme by Anders NorenUp ↑