Created
December 2, 2023 07:36
-
-
Save erdum/8d2a292d264a1b268880a22487c96829 to your computer and use it in GitHub Desktop.
Send JSON response in PHP
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 | |
function send_json($payload, $status_code = 200) | |
{ | |
header('Access-Control-Allow-Origin: *'); | |
header('Content-Type: application/json'); | |
is_callable('http_send_status') ? http_send_status($status_code) | |
: header("HTTP/1.1 $status_code"); | |
return exit(json_encode((object) $payload)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment