new system prototype
This commit is contained in:
24
lib/response.php
Normal file
24
lib/response.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
function json_success(mixed $data, int $status = 200): never {
|
||||
http_response_code($status);
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
echo json_encode(["ok" => true, "data" => $data]);
|
||||
exit;
|
||||
}
|
||||
|
||||
function json_error(string $code, string $message, int $status = 400): never {
|
||||
http_response_code($status);
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
echo json_encode(["ok" => false, "error" => ["code" => $code, "message" => $message]]);
|
||||
exit;
|
||||
}
|
||||
|
||||
function read_json_body(): array {
|
||||
$raw = file_get_contents('php://input');
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data)) {
|
||||
json_error('INVALID_BODY', 'Request body must be valid JSON', 400);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
Reference in New Issue
Block a user