PHP
Public
PHP JSON Response Helper
Return a JSON response from a small PHP endpoint.
#php
#api
#json
#response
PHP
<?php
function json_response(array $payload, int $status = 200): void
{
http_response_code($status);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
json_response([
'success' => true,
'message' => 'Saved successfully.',
]);
Notes
Set the status code before outputting JSON.