<?php
// set header
header('Content-Type: application/json');
// create a php array
$myarr = [
[ "id" => 1, "name" => "Math" ],
[ "id" => 1, "name" => "physics" ],
[ "id" => 1, "name" => "Chemistry" ]
];
// convert to json and print
echo json_encode($myarr);
?>
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