我正在编写一项服务,以接收来自家中能源监控设备的数据。设备可以推送数据的唯一方法是使用 HTTP POST
,使用 Transfer-Encoding: chunked
,没有 Content-Length
,并且请求正文中的数据作为 XML。
我希望能够在 PHP 脚本(在 Apache 后面)中读取这些数据并将其存储在我的数据库中。
看来,目前PHP中读取HTTP请求体最正确的方式是打开读取php://input
。然而,PHP 网站上用户的评论表明(并且我的测试证实),如果客户端未指定 Content-Length
header ,则 php://input
返回 no数据(即使客户端以分块编码发送数据)。
是否有其他方法可以访问请求正文?我可以配置 Apache(使用 .htaccess)来解码分块数据并在获取所有数据后调用我的脚本,并包含 Content-Length
吗?或者配置 PHP 使其能够处理这个问题?
谢谢!
最佳答案
不妨把这个写成我想的答案。
您描述的问题记录在此处:https://bugs.php.net/bug.php?id=60826
线程的亮点:
So here is what I found. If you send chunked http request but do not send the content-length (as we should do using HTTP 1.1 protocol) the body part of the request is empty (php://input returns nothing). This result remains valid for the following configuration where PHP runs via FastCGI :
The same configuration with PHP running as mod_php is fine, the body request is available through php://input.
和
Essentially, PHP is following the spec, and not reading beyond the FastCGI CONTENT_LENGTH header value - so correct fixes are necessarily above PHP, in the FastCGI or web server implementation.
可能的解决方法:
0) 将 httpd 与 mod_php 一起使用
1) 使用不同的网络服务器。
2) 查看 mod_fcgid 的补丁
3) 也许(不推荐)翻转 always_populate_raw_post_data
并查看 $HTTP_RAW_POST_DATA 中是否有任何内容
关于PHP:如何使用传输编码读取 POST 正文:分块,无内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30924004/