APIRest::returnResponse PHP Method

returnResponse() public method

Generic function to send a message and an http code to client
public returnResponse ( $response, $httpcode = 200, $aditionnalheaders = [] )
$response string message or array of data to send
$httpcode integer http code (default 200) (see: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)
$aditionnalheaders array headers to send with http response (must be an array(key => value))
    public function returnResponse($response, $httpcode = 200, $aditionnalheaders = array())
    {
        if (empty($httpcode)) {
            $httpcode = 200;
        }
        foreach ($aditionnalheaders as $key => $value) {
            header("{$key}: {$value}");
        }
        http_response_code($httpcode);
        self::header($this->debug);
        if ($response !== null) {
            $json = json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | ($this->debug ? JSON_PRETTY_PRINT : 0));
        } else {
            $json = '';
        }
        if ($this->debug) {
            echo "<pre>";
            var_dump($response);
            echo "</pre>";
        } else {
            echo $json;
        }
        exit;
    }