phpjsonrestflightphp

PHP REST Service is really slow


I'm having a problem with my PHP REST service, it is really slow. I build it with the Flight PHP Framework. It's accessing data from a MySQL database and returns them as json.

Flight REST Service:

Flight::route('GET /categories', function(){
    header('Content-type: application/json');
    $db = Flight::db();
    $sql = "SELECT * FROM categories";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $result = $stmt->fetchAll();
    echo json_encode($result);
    $db = null;
});

The first response is very fast and the answer is returned not formatted. enter image description here

After ~3 seconds the request is finished and the answer is displayed correctly. enter image description here

Why does it takes so long for finishing the request?

Thanks in advance!


Solution

  • Finally I fixed that behavior with an exit(); call at the end of the method:

    Flight::route('GET /categories', function(){
       header('Content-type: application/json');
       $db = Flight::db();
       $sql = "SELECT * FROM categories";
       $stmt = $db->prepare($sql);
       $stmt->execute();
       $result = $stmt->fetchAll();
       echo json_encode($result);
       $db = null;
       exit();
    });
    

    It takes between 50-80ms for finishing the request now.