PHP:如何使用传输编码读取 POST 正文:分块,无内容长度

标签 php apache http-headers

我正在编写一项服务,以接收来自家中能源监控设备的数据。设备可以推送数据的唯一方法是使用 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/

相关文章:

php - 我想通过 php 将图像作为 blob 类型存储在 mysql 数据库上

php - 如何同时执行多个 Guzzle 请求?

php - 配置文件错误 EasyPHP 和 WAMP 不工作

java - 在 web 服务器中提供静态内容,在 tomcat 中提供动态内容仍然是一个很好的性能实践吗?

php - 使用 PHP 从 Apache 服务器运行 Python 脚本

ASP.NET MVC 和 IE 缓存 - 操作响应 header 无效

php - 数据库 Codeigniter 4 中的图像不显示

php - 注册时间字段前后 10 分钟的容差

php - 当访问者从谷歌搜索到我的网站时,我如何获得搜索词?

c# - 获取网页上图片的大小