如何获取没有内容长度的请求主体?

时间:2014-04-01 11:38:35

标签: php apache http-headers

当请求中未包含内容长度标题时,有没有办法检索请求正文?

例如,我在/var/www/test.php中有这个:

print_r(apache_request_headers());
$data = file_get_contents('php://input');
var_dump($data);

运行此命令:

curl -X POST -d test --header 'Content-Length: ' localhost/test.php

我看到没有内容长度标头,$ data为空。例如,如果我指定内容长度为' 3,我会获得' tes'的数据输出。

有没有办法让php检索请求体,而不管内容长度标题?

1 个答案:

答案 0 :(得分:0)

  

有没有办法在内容长度时检索请求正文   标头未包含在请求中?

是。但是 包含在请求中。

curl -d test localhost/test.php应该返回:

Array
(
    [User-Agent] => curl/7.xx.x
    [Host] => localhost
    [Accept] => */*
    [Content-Length] => 4
    [Content-Type] => application/x-www-form-urlencoded
)

string(4) "test"

您可以省略发送标题。