Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

http - How to pass POST data to the PHP-CGI?

Update:

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

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

<?php var_dump($_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.

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.

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


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);

?>

...which yielded the output:

NULL

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


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

SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I finally figured it out:

Apparently the CONTENT_LENGTH environment variable needs to be set.

Adding:

CONTENT_LENGTH=6
export CONTENT_LENGTH

to my example above causes it to work properly!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...