Update: 2026-05-03 18:15:49
This commit is contained in:
@@ -1,18 +1,43 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Standardized JSON Responses
|
* Standardized JSON Responses with Integrated Logging
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
function json_response(bool $success, $data = null, ?string $message = null, int $code = 200) {
|
function json_response(bool $success, $data = null, ?string $message = null, int $code = 200) {
|
||||||
|
// 1. Logging Logic
|
||||||
|
$logEntry = sprintf(
|
||||||
|
"[%s] %s %s | Code: %d | Success: %s | Message: %s | Data: %s\n",
|
||||||
|
date('Y-m-d H:i:s'),
|
||||||
|
$_SERVER['REQUEST_METHOD'] ?? 'CLI',
|
||||||
|
$_SERVER['REQUEST_URI'] ?? '',
|
||||||
|
$code,
|
||||||
|
$success ? 'YES' : 'NO',
|
||||||
|
$message ?? 'N/A',
|
||||||
|
json_encode($data, JSON_UNESCAPED_UNICODE)
|
||||||
|
);
|
||||||
|
|
||||||
|
$logDir = STORAGE_PATH . '/logs';
|
||||||
|
$logFile = $logDir . '/app.log';
|
||||||
|
|
||||||
|
if (!is_dir($logDir)) {
|
||||||
|
mkdir($logDir, 0755, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@file_put_contents($logFile, $logEntry, FILE_APPEND);
|
||||||
|
|
||||||
|
// 2. HTTP Response
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
http_response_code($code);
|
http_response_code($code);
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'success' => $success,
|
'success' => $success,
|
||||||
'data' => $data,
|
'data' => $data,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
'timestamp' => date('c')
|
'timestamp' => date('c')
|
||||||
], JSON_UNESCAPED_UNICODE);
|
], JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user