Skip to content

Instantly share code, notes, and snippets.

@erdum
Created December 2, 2023 07:36
Show Gist options
  • Save erdum/8d2a292d264a1b268880a22487c96829 to your computer and use it in GitHub Desktop.
Save erdum/8d2a292d264a1b268880a22487c96829 to your computer and use it in GitHub Desktop.
Send JSON response in PHP
<?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