DEV Community

Shaswat Raj
Shaswat Raj

Posted on

Returning JSON from a PHP Script - Create Rest API in Php

While you're usually fine without it, you can and should set the Content-Type header:

<?php
$data = [];/** whatever you're serializing **/;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
?>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)