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