Returning JSON from a PHP Script

前端 未结 18 1443
南方客
南方客 2020-11-21 04:59

I want to return JSON from a PHP script.

Do I just echo the result? Do I have to set the Content-Type header?

18条回答
  •  忘了有多久
    2020-11-21 05:46

    You can use this little PHP library. It sends the headers and give you an object to use it easily.

    It looks like :

    test = 'OK';
    $arraytest = array('1','2','3');
    $jsonOnly = '{"Hello" : "darling"}';
    
    // Add some content
    $Json->add('width', '565px');
    $Json->add('You are logged IN');
    $Json->add('An_Object', $object);
    $Json->add("An_Array",$arraytest);
    $Json->add("A_Json",$jsonOnly);
    
    // Finally, send the JSON.
    
    $Json->send();
    ?>
    

提交回复
热议问题