The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Get http response and then continue process

Hardik Dave's Avatar

Hardik Dave

18 Aug, 2015 07:34 AM

I am sending a downloadable file url to slim restful api. Once the file is downloaded on my server, I want to send the user a response as
{status:"done"} and then start processing in the backend.

i.e. I want to echo the response and then initiate my process on the server. I am new to slim and have been trying redirect ob_flush etc. Could anyone please help?

  1. 1 Posted by Rob Allen on 18 Aug, 2015 08:47 AM

    Rob Allen's Avatar

    I don't think you can do what you want to do as the PHP process finishes when the response is sent to the browser.

    The usual solution to this problem is to use a queue like RabbitMQ or Gearman

  2. 2 Posted by Hardik Dave on 18 Aug, 2015 01:05 PM

    Hardik Dave's Avatar

    Sir, I have got Gearman, I am able to run background jobs and normal ones too. I wanted to know:
    1. How does gearman queue request?
    2. Could I get the status of a particular request (Queued, Processing,Completed)?
    3. How can rate limiting be executed?

    I am trying to do the following:

    1. Send file url - and response to the user
    2. Processing the file - long running jobs (should be queued if needed)
    3. GET status of the process

    Any help to startoff would be appreciated.

  3. 3 Posted by Pablo on 23 Jun, 2016 10:36 AM

    Pablo's Avatar

    My solution:

    As a middleware:

    $this->_app->post('/api/route',
                $this->responseAndContinue('whatever_content' , 200),
                function () use ($app) {
                 //Code ......
                }

    //Middleware

    private function finishHere($content,$status){
            ob_end_clean();
            ob_end_flush(); // This line does the trick
            header("Connection: close");
            header("HTTP/1.0 $status " . HttpStatusCode::get($status));
            header('Content-type: application/json; charset=utf-8');
            ignore_user_abort(); // optional
            ob_start();
            echo $content;
            $size = ob_get_length();
            header("Content-Length: $size");
            ob_end_flush(); // Again for print content
            flush(); // Unless both are called !
            session_write_close();
        }

        /**
         * Finish response and continue
         * @return session finish
         */
        protected function responseAndContinue($content,$status){
            return function ($route) use ($content,$status) {
               $this->finishHere($content,$status);
            };
        }

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac