好的,所以Dave和Everts的建议我决定手动解析原始请求数据。搜索约一天后,我没有找到任何其他方式来做到这一点。
我从这个thread获得了一些帮助。我没有任何运气篡改原始数据,就像在引用的线程中一样,因为这将破坏正在上传的文件。所以这是正则表达式。这没有很好的测试,但似乎在为我的工作案例工作。不用多说,希望这有可能有助于别人:
function parse_raw_http_request(array &$a_data)
{
// read incoming data
$input = file_get_contents('php://input');
// grab multipart boundary from content type header
preg_match('/boundary=(.*)$/', $_SERVER['CONTENT_TYPE'], $matches);
$boundary = $matches[1];
// split content by boundary and get rid of last -- element
$a_blocks = preg_split("/-+$boundary/", $input);
array_pop($a_blocks);
// loop data blocks
foreach ($a_blocks as $id => $block)
{
if (empty($block))
continue;
// you'll have to var_dump $block to understand this and maybe replace \n or \r with a visibile char
// parse uploaded files
if (strpos($block, 'application/octet-stream') !== FALSE)
{
// match "name", then everything after "stream" (optional) except for prepending newlines
preg_match("/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s", $block, $matches);
}
// parse all other fields
else
{
// match "name" and optional value in between newline sequences
preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $block, $matches);
}
$a_data[$matches[1]] = $matches[2];
}
}
通过引用使用(为了不复制数据太多):
$a_data = array();
parse_raw_http_request($a_data);
var_dump($a_data);
编辑:看到下面的Jas答案,他增加了对多个文件和一些其他功能的支持。