[ACCEPTED]-Returning a JSON result from a PHP REST web service-curl
Accepted answer
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
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
exit(json_encode($result_json));
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...
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.