如何使用REST API和PHP发送代码200和403

我使用php和restful api开发了一个小型Crud应用程序。如果要创建记录,我想发送代码200,而错误消息则发送代码403。下面是我的代码:

 TASK [services-pre-install-checks : List out PROC Stats on DGX/GPU Nodes Nodes] ***
16:21:57  Wednesday 06 November 2019  00:21:57 +0000 (0:00:03.628)       0:00:19.398 **** 
16:21:59  failed: [localhost -> hostA.test.metal.net] (item=hostA) => {"ansible_loop_var": "item","changed": true,"cmd": ["cat","/proc/fs/fscache/stats","|","grep","FS"],"delta": "0:00:00.106478","end": "2019-11-06 00:21:59.372137","failed_when_result": true,"item": "hostA","msg": "non-zero return code","rc": 1,"start": "2019-11-06 00:21:59.265659","stderr": "cat: '|': No such file or directory\ncat: grep: No such file or directory\ncat: FS: No such file or directory","stderr_lines": ["cat: '|': No such file or directory","cat: grep: No such file or directory","cat: FS: No such file or directory"],"stdout": "FS-Cache statistics(ver:1.0)\nCookies: idx=613 dat=520 spc=0\nObjects: alc=3 nal=0 avl=3 ded=4\nChkAux : non=0 ok=1 upd=0 obs=0\nPages  : mrk=1 unc=1\nAcquire: n=1133 nul=0 noc=0 ok=1133 nbf=0 oom=0\nLookups: n=3 neg=2 pos=1 crt=2 tmo=0\nInvals : n=0 run=0\nUpdates: n=0 nul=0 run=0\nRelinqs: n=1133 nul=0 wcr=0 rtr=0\nAttrChg: n=0 ok=0 nbf=0 oom=0 run=0\nAllocs : n=0 ok=0 wt=0 nbf=0 int=0\nAllocs : ops=0 owt=0 abt=0\nRetrvls: n=520 ok=0 wt=1 nod=1 nbf=519 int=0 oom=0\nRetrvls: ops=1 owt=1 abt=0\nStores : n=1 ok=1 agn=0 nbf=0 oom=0 wrxd=0 sol=0\nStores : ops=1 run=2 pgs=1 rxd=1 irxd=0 olm=0 ipp=0\nVmScan : nos=0 gon=0 bsy=0 can=0 wt=0\nOps    : pend=1 run=2 enq=2 can=0 rej=0\nOps    : ini=2 dfr=0 rel=2 gc=0\nCacheOp: alo=0 luo=0 luc=0 gro=0\nCacheOp: inv=0 upo=0 dro=0 pto=0 atc=0 syn=0\nCacheOp: rap=0 ras=0 alp=0 als=0 wrp=0 ucp=0 dsp=0\nCacheEv: nsp=0 stl=0 rtr=0 cul=0","stdout_lines": ["FS-Cache statistics(ver:1.0)","Cookies: idx=613 dat=520 spc=0","Objects: alc=3 nal=0 avl=3 ded=4","ChkAux : non=0 ok=1 upd=0 obs=0","Pages  : mrk=1 unc=1","Acquire: n=1133 nul=0 noc=0 ok=1133 nbf=0 oom=0","Lookups: n=3 neg=2 pos=1 crt=2 tmo=0","Invals : n=0 run=0","Updates: n=0 nul=0 run=0","Relinqs: n=1133 nul=0 wcr=0 rtr=0","AttrChg: n=0 ok=0 nbf=0 oom=0 run=0","Allocs : n=0 ok=0 wt=0 nbf=0 int=0","Allocs : ops=0 owt=0 abt=0","Retrvls: n=520 ok=0 wt=1 nod=1 nbf=519 int=0 oom=0","Retrvls: ops=1 owt=1 abt=0","Stores : n=1 ok=1 agn=0 nbf=0 oom=0 wrxd=0 sol=0","Stores : ops=1 run=2 pgs=1 rxd=1 irxd=0 olm=0 ipp=0","VmScan : nos=0 gon=0 bsy=0 can=0 wt=0","Ops    : pend=1 run=2 enq=2 can=0 rej=0","Ops    : ini=2 dfr=0 rel=2 gc=0","CacheOp: alo=0 luo=0 luc=0 gro=0","CacheOp: inv=0 upo=0 dro=0 pto=0 atc=0 syn=0","CacheOp: rap=0 ras=0 alp=0 als=0 wrp=0 ucp=0 dsp=0","CacheEv: nsp=0 stl=0 rtr=0 cul=0"]}

这是我创建记录的功能:

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];

$query = $restObj->create_record($firstname,$lastname,$age);
echo json_encode([
      "message" => $query ? "User Created" : "Error creating user"
]);

代码工作正常,可以创建记录,但是我不知道如何显示代码200和代码403消息。请帮助

jialinjl 回答:如何使用REST API和PHP发送代码200和403

您可以使用http_response_code(200)获取并设置HTTP响应代码。

请参阅PHP Manual: http-response-code

本文链接:https://www.f2er.com/3155632.html

大家都在问