"returning json from a php script" Code Answer

1

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');
echo json_encode($data);

if i'm not using a particular framework, i usually allow some request params to modify the output behavior. it can be useful, generally for quick troubleshooting, to not send a header, or sometimes print_r the data payload to eyeball it (though in most cases, it shouldn't be necessary).

By sacredfaith on January 27 2022

Answers related to “returning json from a php script”

Only authorized users can answer the Search term. Please sign in first, or register a free account.