Last active
March 27, 2023 15:32
-
-
Save jairusjoer/7541f79e03293005e2a29272f724d535 to your computer and use it in GitHub Desktop.
Accept POST data in JSON format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$response = (object) []; | |
$json = json_decode(file_get_contents('php://input')); | |
//... | |
if (!$json) { | |
$response->state = [ | |
'code' => 400, | |
'type' => 'error', | |
'message' => 'Something went wrong. Please try again' | |
]; | |
http_response_code($response->state['code']); | |
echo json_encode($response); | |
exit; | |
} | |
//... | |
$response->state = [ | |
'code' => 200, | |
'type' => 'success', | |
'message' => 'Thank you for your submission', | |
]; | |
http_response_code($response->state['code']); | |
echo json_encode($response); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment