php - 从 PHP REST Web 服务返回 JSON 结果

标签 php web-services json rest curl

我有一个用 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/

相关文章:

javascript - 从 Google+ 相册中获取 JSON 格式的照片

php - 如何根据where条件值创建mysql查询结果?

PHP:PDF生成

c# - .designer 文件未与 Visual Studio 中的 .cs 文件关联?

web-services - 如何同时运行Apache(httpd)和Tomcat?

php - 如何在php中解析mysql中的空间数据

php - 如何向本教程添加一些 php,包括 mysql 查询?

php - 启用了 MultiViews 的 Apache Clean-Urls

c# - 编写 C# 客户端以使用返回对象数组的 Java Web 服务

json - 后端只接受纯文本,不接受 JSON,只接受名称而不是 id