但是如果有一个PUT,PATCH,或任何请求类型,其他 比POST?
好吧,既然你是一个设计的API,然后你是谁决定是否它仅接受POST
,PUT
,POST
+ PUT
或请求头的任何其他组合的一个。
该API不应被设计为“接受并尝试处理”第三方应用程序提交给您的API的所有内容。这是应用程序的工作(我的意思是,连接到API的应用程序)以这种方式准备请求,API接受它。请注意,启用多个请求方法(特别是那些必须以不同方式处理的请求方法)有多种处理请求的方式(例如安全性,类型等)。 这基本上意味着要么巧妙地设计一个请求处理过程,要么会遇到不同请求类型调用的API方法的问题,这会很麻烦。
如果您需要获取请求的原始内容 - @Adil Abbasi似乎在正确的轨道上(就解析php://input
而言)。 但请注意,php://input
不适用于enctype =“multipart/form-data”as described in the docs。
<?php
$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
,并尝试这样的:
<?php
$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");
}
我测试过和'PHP: // input'可与PUT请求,“根据文档它不是在PHP可用://输入”没有看到任何东西contradictious在文档 – cske
- 我看不出它说,在文档。 – Quentin
检查这个链接,如果它是有帮助的,[链接](http://stackoverflow.com/questions/9464935/php-multipart-form-data-put-request)。 –