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);
?>


This content originally appeared on DEV Community and was authored by Shaswat Raj

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);
?>


This content originally appeared on DEV Community and was authored by Shaswat Raj


Print Share Comment Cite Upload Translate Updates
APA

Shaswat Raj | Sciencx (2022-01-24T06:41:01+00:00) Returning JSON from a PHP Script – Create Rest API in Php. Retrieved from https://www.scien.cx/2022/01/24/returning-json-from-a-php-script-create-rest-api-in-php/

MLA
" » Returning JSON from a PHP Script – Create Rest API in Php." Shaswat Raj | Sciencx - Monday January 24, 2022, https://www.scien.cx/2022/01/24/returning-json-from-a-php-script-create-rest-api-in-php/
HARVARD
Shaswat Raj | Sciencx Monday January 24, 2022 » Returning JSON from a PHP Script – Create Rest API in Php., viewed ,<https://www.scien.cx/2022/01/24/returning-json-from-a-php-script-create-rest-api-in-php/>
VANCOUVER
Shaswat Raj | Sciencx - » Returning JSON from a PHP Script – Create Rest API in Php. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/24/returning-json-from-a-php-script-create-rest-api-in-php/
CHICAGO
" » Returning JSON from a PHP Script – Create Rest API in Php." Shaswat Raj | Sciencx - Accessed . https://www.scien.cx/2022/01/24/returning-json-from-a-php-script-create-rest-api-in-php/
IEEE
" » Returning JSON from a PHP Script – Create Rest API in Php." Shaswat Raj | Sciencx [Online]. Available: https://www.scien.cx/2022/01/24/returning-json-from-a-php-script-create-rest-api-in-php/. [Accessed: ]
rf:citation
» Returning JSON from a PHP Script – Create Rest API in Php | Shaswat Raj | Sciencx | https://www.scien.cx/2022/01/24/returning-json-from-a-php-script-create-rest-api-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.