脚本宝典收集整理的这篇文章主要介绍了php – 使用rest api返回http状态代码,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
public function actiontest() { $rtn=array("id":"3","name":"John"); PRint json_encode($rtn); }
无论如何我都没有操纵标题.从阅读stackoverflow,我知道我应该返回http响应代码以匹配我的api结果.如何构建我的api并返回响应代码.我只是不明白我是怎么做到的,因为现在我只是打印出一个json.
public function actiontest() { try { // Here: everything went ok. So before returning JSON,you can SETUP HTTP status code too $rtn = array("id","3","name","John"); http_response_code(200); print json_encode($rtn); } catch (SomeException $ex) { $rtn = array("id","error","something wrong hapPEned"); http_response_code(500); print json_encode($rtn); } }
基本上,在流输出(JSON数据)之前,您可以通过http_response_code($code)函数设置HTTP状态代码.
以上是脚本宝典为你收集整理的php – 使用rest api返回http状态代码全部内容,希望文章能够帮你解决php – 使用rest api返回http状态代码所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。