/**
  * Returns data as a result of API call.
  *
  * @param mixed $result Result.
  */
 public static function sendData($result)
 {
     // Encodes result to UTF8.
     Nl2goManager::utf8Encode($result);
     // Create ok response.
     $response = array('status' => 'ok', 'data' => $result);
     // Return result in json form.
     echo json_encode($response);
     // Stop rendering contact form.
     exit;
 }
<?php

require_once __DIR__ . DIRECTORY_SEPARATOR . 'nl2go_manager.php';
$api = filter_input(INPUT_POST, 'api');
// Ensure this is API call.
if ($api !== 'nl2go') {
    return;
}
$method = filter_input(INPUT_POST, 'method');
// Ensure that method is supported.
if ($method === null || array_search($method, Nl2goManager::$supportedMethods) === false) {
    Nl2goManager::sendError('METHODNOTFOUND');
}
// Check if user is authenticated.
Nl2goManager::checkAuthentication();
// Call method.
$apiManager = new Nl2goManager();
$result = $apiManager->{$method}();
// Returns JSON response
Nl2goManager::sendData($result);