victorwesterlund.com/api/router.php
Victor Westerlund 790d65be6d Added core API router
Added basic PHP script to route and handle exceptions from API calls.
2020-12-21 15:55:13 +01:00

29 lines
No EOL
547 B
PHP

<?php
try {
$file = $_SERVER["REQUEST_URI"].".php";
if(file_exists($file)) {
require $file;
return;
}
$api_root = explode("/",$_SERVER["REQUEST_URI"])[1];
if(is_dir($api_root) && file_exists($api_root."/default.php")) {
require $api_root."/default.php";
return;
}
throw new Exception("Invalid API");
} catch(Exception $error) {
http_response_code("400");
header("Content-Type: application/json");
$output = [
"status" => "error",
"message" => $error->getMessage()
];
echo json_encode($output);
}