Epi*_*rus 4 php rest json curl web-services
我有一个用PHP编写的REST Web服务,我使用POST请求调用它(为此使用curl).Web服务应返回JSON文档.问题是,我不确定将此文档发送回Web服务客户端的正确方法是什么.只是回应它就足够了吗?
现在,看起来这是我可以让JSON文档出现在POST请求结果中的唯一方法($ result变量):
$result = curl_exec($ch);
Run Code Online (Sandbox Code Playgroud)
小智 18
您可以在Array或Object中格式化结果,然后使用json标头回显它.即
$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);
Run Code Online (Sandbox Code Playgroud)
希望这有帮助,谢谢