But what if there is a PUT, PATCH, or whatever request type, other
than POST?
好吧,既然您是设计API的人,那么您就决定是否只接受POST,PUT,POST PUT或任何其他请求标头组合.
API不应设计为“接受并尝试处理”第三方应用提交给您的API的所有内容.应用程序的工作(我的意思是,连接到API的应用程序)以这种方式准备请求,API接受它.
请记住,启用多个请求方法(尤其是那些必须以不同方式处理的方法)会有多种方法来处理请求(例如安全性,类型等).
它基本上意味着您必须巧妙地设计请求处理过程,否则您将遇到使用不同请求类型调用的API方法的问题,这将很麻烦.
如果您需要获取请求的原始内容 – @Adil Abbasi似乎在正确的轨道上(就解析php://输入而言).但请注意,php://输入不适用于enctype =“multipart / form-data”as described in the docs.
$input = file_get_contents('php://input');
// assuming it's JSON you allow - convert json to array of params
$requestParams = json_decode($input, true);
if ($requestParams === FALSE) {
// not proper JSON received - set response headers properly
header("HTTP/1.1 400 Bad Request");
// respond with error
die("Bad Request");
}
// proceed with API call - JSON parsed correctly
如果你需要使用enctype =“multipart / form-data” – 请阅读I/O Streams docs中的STDIN,并尝试这样:
$bytesToRead = 4096000;
$input = fread(STDIN, $bytesToRead ); // reads 4096K bytes from STDIN
if ($input === FALSE) {
// handle "failed to read STDIN"
}
// assuming it's json you accept:
$requestParams = json_decode($input , true);
if ($requestParams === FALSE) {
// not proper JSON received - set response headers properly
header("HTTP/1.1 400 Bad Request");
// respond with error
die("Bad Request");
}