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

2023-10-12php开发问题
0

本文介绍了如何将 POST 数据传递给 PHP-CGI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

更新:

出于绝望,我在 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!

这篇关于如何将 POST 数据传递给 PHP-CGI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17