e949/api/_json.php

45 lines
1.1 KiB
PHP

<?php // JSON-related functions
// Includes
if ($IS_FRONTEND)
require_once("api/_errorslist.php");
else
require_once("_errorslist.php");
// Write valid JSON data to stdout and exit
function JSON_ReturnData ($arr) {
$data = json_encode($arr);
if (!$data) {
$data = json_encode(
array(
"error" => Errors_ResolveNameByCode(E_UNS_JSONBADINP)
)
);
}
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
));
}
?>