php 解析http,用PHP手动解析原始HTTP数据
好的,所以Dave和Everts的建議我決定手動解析原始請求數(shù)據(jù)。搜索約一天后,我沒有找到任何其他方式來做到這一點。
我從這個thread獲得了一些幫助。我沒有任何運氣篡改原始數(shù)據(jù),就像在引用的線程中一樣,因為這將破壞正在上傳的文件。所以這是正則表達式。這沒有很好的測試,但似乎在為我的工作案例工作。不用多說,希望這有可能有助于別人:
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];
}
}
通過引用使用(為了不復制數(shù)據(jù)太多):
$a_data = array();
parse_raw_http_request($a_data);
var_dump($a_data);
編輯:看到下面的Jas答案,他增加了對多個文件和一些其他功能的支持。
總結(jié)
以上是生活随笔為你收集整理的php 解析http,用PHP手动解析原始HTTP数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《一粒红尘》闵朗喜欢谁 他的结局和谁在一
- 下一篇: php论坛思路,PHP论坛实现积分系统的