Skip to content

Instantly share code, notes, and snippets.

@jairusjoer
Created September 24, 2022 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jairusjoer/a0eeb5122d667b9f06082ab75dd36875 to your computer and use it in GitHub Desktop.
Save jairusjoer/a0eeb5122d667b9f06082ab75dd36875 to your computer and use it in GitHub Desktop.
A simple JSON response for PHP endpoints with corresponding response code
<?php
$response = new \stdClass();
$response->state = [
'code' => 200,
'type' => 'success', // Can be used for CSS indicators classes such as `.is-[type]`
'message' => 'Your data has ben successfully submitted.'
];
header('Content-type: application/json');
http_response_code($response->state['code']);
echo json_encode($response);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment