jeudi 28 mars 2019

Execute php code after giving the user 200 response

I am building an API. In it, there is some code that does not need to be executed for giving a response to the user. So I would like to give the response after the mandatory code is executed and also run the remaining code in the background.

This is a Linux server running apache and PHP7.2

        ignore_user_abort(true);
        set_time_limit(0);

        ob_start();

        $api_key = $request->get('api_key');
        $clientToken = ClientToken::where('token', $api_key)->where('is_blocked', 0)->get();
        if (count($clientToken) > 0) {
            $dateTime = date_create_from_format('d-m-Y H:i:s a', $request->get('date_time'));
            $missCallRequest = new MissCallRequest();
            $missCallRequest->client_token_id = $clientToken[0]->id;
            $missCallRequest->called_number = "+" . $request->get('called_number');
            $missCallRequest->caller_number = $request->get('caller_number');
            $missCallRequest->datetime = date_format($dateTime, 'Y-m-d H:i:s');
            $missCallRequest->operator = $request->get('operator');
            $missCallRequest->circle = $request->get('circle');
            $missCallRequest->request_url = urldecode($request->fullUrl());
            $missCallRequest->save();

        }

        $response = [
            "status" => 200,
            "message" => "Request Received",
        ];

        echo json_encode($response); // send the 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.
        ob_end_flush();
        ob_flush();
        flush();

        echo "hello";

This is responding after the mandatory code but it's not executing the rest of the code after responding.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire