What is the method for PHP to return json xml jsonp ?
The method of php returning json: 1. Set “header(‘Content-Type: application/json; charset=utf-8’);” in the php file; 2. Create “array(‘a’=>1,’b ‘=>2);”; 3. Just return the json data through “exit(json_encode($data));”.
How does PHP return json data?
PHP returns data in json, xml, JSONP and other formats
- Return json data:
header('Content-Type:application/json; charset=utf-8');
$arr = array('a'=>1,'b'=>2);
exit(json_encode($data));
Note: If you directly output the value of json_encode without adding a header, the returned string is not an object, and js needs to convert it into an object by eval(‘(‘+data+’)’) first, and then get the value.
Return xml data:
header('Content-Type:text/xml; charset=utf-8');
exit($xml);
Return jsonp data:
$arr = array('a'=>1, 'b'=>2, 'c'=>3);
$json = json_encode($arr);
$callback = $_GET['callback'];
exit($callback."($json)");
Use count() for length
$sjson = json_decode($res);
for($i=0;$i<count($sjson);$i++)
{
echo $sjson[$i];
}