Chr*_*tof 30 php parsing curl http
我似乎无法找到这个问题的真正答案,所以我在这里:
如何multipart/form-data
在PHP中以格式解析原始HTTP请求数据?我知道如果格式正确,原始POST会自动解析,但我所指的数据来自PUT请求,而PHP不会自动解析.数据是多部分的,看起来像:
------------------------------b2449e94a11c
Content-Disposition: form-data; name="user_id"
3
------------------------------b2449e94a11c
Content-Disposition: form-data; name="post_id"
5
------------------------------b2449e94a11c
Content-Disposition: form-data; name="image"; filename="/tmp/current_file"
Content-Type: application/octet-stream
?????JFIF?????????... a bunch of binary data
Run Code Online (Sandbox Code Playgroud)
我正在使用libcurl发送数据(伪代码):
curl_setopt_array(
CURLOPT_POSTFIELDS => array(
'user_id' => 3,
'post_id' => 5,
'image' => '@/tmp/current_file'),
CURLOPT_CUSTOMREQUEST => 'PUT'
);
Run Code Online (Sandbox Code Playgroud)
如果我删除了CURLOPT_CUSTOMREQUEST位,请求将作为服务器上的POST处理,并且所有内容都被解析得很好.
有没有办法手动调用PHP的HTTP数据解析器或其他一些很好的方法来做到这一点?是的,我必须发送请求作为PUT :)
Chr*_*tof 31
好的,所以有了Dave和Everts的建议,我决定手动解析原始请求数据.在搜索了大约一天之后,我没有找到任何其他方法来做到这一点.
我从这个帖子得到了一些帮助.我没有像在引用的线程中那样篡改原始数据,因为这会破坏正在上传的文件.所以这都是正则表达式.这并没有很好地测试,但似乎适用于我的工作案例.没有进一步的麻烦,并希望有一天这可能会帮助别人:
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];
}
}
Run Code Online (Sandbox Code Playgroud)
通过引用使用(为了不过多地复制数据):
$a_data = array();
parse_raw_http_request($a_data);
var_dump($a_data);
Run Code Online (Sandbox Code Playgroud)
编辑:这个答案在7年后仍然定期点击.从那以后我从未使用过这段代码,现在也不知道是否有更好的方法.请查看下面的评论,并了解有许多情况下此代码无效.使用风险由您自己承担.
我很惊讶没有人提到parse_str
或mb_parse_str
:
$result = [];
$rawPost = file_get_contents('php://input');
mb_parse_str($rawPost, $result);
var_dump($result);
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/function.mb-parse-str.php
我使用了Chris的示例函数,并添加了一些必需的功能,例如R Porter对$ _FILES数组的需要。希望它对某些人有帮助。
<?php
include_once('class.stream.php');
$data = array();
new stream($data);
$_PUT = $data['post'];
$_FILES = $data['file'];
/* Handle moving the file(s) */
if (count($_FILES) > 0) {
foreach($_FILES as $key => $value) {
if (!is_uploaded_file($value['tmp_name'])) {
/* Use getimagesize() or fileinfo() to validate file prior to moving here */
rename($value['tmp_name'], '/path/to/uploads/'.$value['name']);
} else {
move_uploaded_file($value['tmp_name'], '/path/to/uploads/'.$value['name']);
}
}
}
Run Code Online (Sandbox Code Playgroud)