php原生form支持put,在PUT,PATCH,DELETE …请求中使用php读取multipart / form-data的原始请求体...

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");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值