[ACCEPTED]-Returning a JSON result from a PHP REST web service-curl

Accepted answer
Score: 23

You can format your result in Array or Object 2 and then Just echo it with the json headers. i.e

$result_json = array('name' => 'test', 'age' => '16');

// headers for not caching the results
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

// headers to tell that result is JSON
header('Content-type: application/json');

// send the result now
echo json_encode($result_json);

Hope 1 this helps, Thanks

Score: 1

To get json result from php you should use

echo json_encode($result_json)

but 3 echo does not exit the program so after 2 using echo it is better to exit program 1 but for shorthand you can use

Score: 0

I've implemented it a few times and I was 4 posting it just as string to the WS and 3 echoing back from WS as response again as 2 string. I used json_encode and json_decode 1 functions for this...

More Related questions