e949/api/_json.php

42 lines
1.1 KiB
PHP
Raw Normal View History

<?php // JSON-related functions
2023-08-12 04:09:17 +05:30
// Includes
require_once("_errorslist.php");
2023-08-12 04:09:17 +05:30
// Write valid JSON data to stdout and exit
function JSON_ReturnData ($arr) {
2023-08-12 04:09:17 +05:30
$data = json_encode($arr);
if (!$data) {
$data = json_encode(
array(
"error" => Errors_ResolveNameByCode(E_UNS_JSONBADINP)
)
);
2023-08-12 04:09:17 +05:30
}
header("Content-Type: application/json; charset=utf-8");
echo $data;
exit;
}
// Return error as JSON data to stdout and exit
function JSON_ReturnError (int $code = -1, string $name = "", string $desc = "") {
if ($code === -1 && empty($name))
JSON_ReturnError(code: E_UNS_INTERNAL, desc: "cant return error without specified code or name");
else if ($code === -1)
$code = Errors_ResolveCodeByName($name);
else if (empty($name))
$name = Errors_ResolveNameByCode($code);
JSON_ReturnData(array(
"error" => $name, // Name
"error_code" => $code, // Code
"error_hum" => Errors_ResolveDescByCode($code), // Common description
"description" => $desc // Detailed decription
2023-08-12 04:09:17 +05:30
));
}
2023-08-12 04:09:17 +05:30
?>