openstamanager/api/index.php

53 lines
1.2 KiB
PHP
Raw Normal View History

<?php
function serverError()
{
$error = error_get_last();
if ($error['type'] == E_ERROR) {
ob_end_clean();
2019-07-19 15:23:00 +02:00
echo Response::error('serverError');
}
}
// Gestione degli errori
set_error_handler('serverError');
register_shutdown_function('serverError');
include_once __DIR__.'/../core.php';
// Disabilta la sessione per l'API
session_write_close();
// Permesso di accesso all'API da ogni dispositivo
header('Access-Control-Allow-Origin: *');
2018-08-01 15:15:14 +02:00
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
2019-07-19 15:23:00 +02:00
use API\Response;
try {
2019-07-19 15:23:00 +02:00
$response = Response::manage();
} catch (Exception $e) {
2018-07-27 12:17:17 +02:00
// Log dell'errore
$logger = logger();
$logger->addRecord(\Monolog\Logger::ERROR, $e);
2019-07-19 15:23:00 +02:00
$response = Response::error('serverError');
}
2018-08-01 15:15:14 +02:00
// Richiesta OPTIONS (controllo da parte del dispositivo)
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
2019-07-19 15:23:00 +02:00
$response = Response::error('ok');
2018-08-01 15:15:14 +02:00
}
json_decode($response);
2018-06-29 15:57:12 +02:00
// Impostazioni di Content-Type e Charset Header
if (json_last_error() == JSON_ERROR_NONE) {
header('Content-Type: application/json; charset=UTF-8');
} else {
header('Content-Type: text/plain; charset=UTF-8');
}
// Stampa dei risultati
echo $response;