如何将 POST 数据传递给 PHP-CGI?

2022-01-05 00:00:00 http cgi request post php

更新:

出于绝望,我在 shell 中执行了以下操作:

In a fit of desperation, I did the following in a shell:

REDIRECT_STATUS=true
SCRIPT_FILENAME=/var/www/...
REQUEST_METHOD=POST
GATEWAY_INTERFACE=CGI/1.1
export REDIRECT_STATUS
export SCRIPT_FILENAME
export REQUEST_METHOD
export GATEWAY_INTERFACE
echo "test=1" | php-cgi

...并且仍然没有 $_POST 变量出现在这个输出中:

...and STILL no $_POST variables are showing up in the output of this:

<?php var_dump($_POST); ?>

<小时>

我正在尝试创建一个与 php-cgi 二进制文件接口的小型网络服务器.然而,事情并没有那么顺利.php-cgi 二进制文件正确处理 GET 请求.当涉及 POST 请求时,$_POST 数组是空的,即使事情正在被 POST.


I am trying to create a small webserver that interfaces with the php-cgi binary. However, things aren't going so well. The php-cgi binary correctly handles GET requests. When it comes to POST requests, the $_POST array is empty, even when things are getting POSTed.

我检查了输入 php-cgi 二进制文件的 HTTP 标头,它们确实包含 POST 数据和 Content-type: application/x-www-form-urlencoded 标头.

I've checked the HTTP headers being fed into the php-cgi binary and they do indeed include the POST data and the Content-type: application/x-www-form-urlencoded header.

是什么让 php-cgi 二进制文件无法看到请求中包含 POST 数据?

What could be keeping the php-cgi binary from seeing that there's POST data included in the request?

我正在取得进展,我从 PHP 源代码:

I'm making progress, I've dug up some stuff from the PHP source code:

  • /sapi/cgi/cgi_main.c:

468: static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)

(我不知道从哪里调用这个函数.)

(I have no idea where this function is invoked from.)

阅读下面的答案后,我尝试了:

After reading the answer below, I tried:

<?php

var_dump($HTTP_RAW_POST_DATA);

?>

...产生输出:

NULL

...表示有更奇怪的东西在这里工作.

...indicating that something even stranger is at work here.

我越来越近了...我在 /main/php_content_types.c:

I'm getting closer... I found this function in /main/php_content_types.c:

SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader)

SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader)

...它似乎是处理 POST 请求的代码.

...and it seems to be the code that processes POST requests.

推荐答案

我终于想通了:

显然需要设置 CONTENT_LENGTH 环境变量.

Apparently the CONTENT_LENGTH environment variable needs to be set.

添加:

CONTENT_LENGTH=6
export CONTENT_LENGTH

我上面的例子使它正常工作!

to my example above causes it to work properly!

相关文章