<?php
header('Content-Type: application/json');
//First Examaple
$json_data = array("key" => "value");
echo json_encode($json_data);
// More Complex example
$php_arr = [
[ "id" => 1, "name" => "Math" ],
[ "id" => 1, "name" => "physics" ],
[ "id" => 1, "name" => "Chemistry" ]
];
echo json_encode($php_arr);
?>
To return JSON from a PHP script you can set its header to 'Content-Type: application/json'.
This will return data in JSON format as your API response.
0 Comments