我有一个用 PHP 编写的 REST Web 服务,我使用 POST 请求调用它(为此使用 curl)。 Web 服务应返回一个 JSON 文档。 问题是,我不确定将此文档发送回 Web 服务客户端的正确方法是什么。仅仅回显它就足够了吗?
现在看来这是我可以让 JSON 文档出现在 POST 请求($result 变量)结果中的唯一方法:
$result = curl_exec($ch);
最佳答案
您可以将结果格式化为数组或对象,然后使用 json header 回显它。 即
$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);
希望对你有帮助,谢谢
关于php - 从 PHP REST Web 服务返回 JSON 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5719095/