Compare the differences between form POST and fsockopen submission methods.
When the form is submitted in POST mode,
$_POST and php://input can get the value, and $HTTP_RAW_POST_DATA is empty.
$_POST organizes the submitted data in an associative array and performs encoding processing, such as urldecode, and even encoding conversion.
php://input can obtain unprocessed POST raw data by reading files through the input stream.
php://input allows reading the raw data of POST. It puts less pressure on memory than $HTTP_RAW_POST_DATA and does not require any special php.ini settings. php://input cannot be used with enctype="multipart/form-data".
Example of fsockopen submitting POST data:
$sock = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$sock) die("$errstr ($errno)n");
$data = "txt=" . urlencode("中") . "&bar=" . urlencode("Value for Bar");
fwrite($sock, "POST /posttest/response.php HTTP/1.0rn");
fwrite($sock, "Host: localhostrn");
fwrite($sock, "Content-type: application/x-www-form-urlencodedrn");
fwrite($sock, "Content-length: " . strlen($data) . "rn");
fwrite($sock, "Accept: */*rn");
fwrite($sock, "rn");
fwrite($sock, "$datarn");
fwrite($sock, "rn");
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$strn";
echo "n";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
echo $body;
is consistent with the result of (1)
:
1. Use php://input to easily get the original POST data.
2. $HTTP_RAW_POST_DATA is only valid when the Content-Type type of POST is not recognized by PHP.
For example, POST data usually submitted through page forms cannot be passed through $ HTTP_RAW_POST_DATA is extracted. Its encoding type attribute (enctype attribute) is application/x-www-form-urlencoded, multipart/form-data.
Note: Even if you explicitly change the enctype attribute in the page to a type that is not recognized by PHP, it will still be invalid.
Since the form submission encoding attribute is form-limited, unrecognizable types will be considered to be submitted in the default encoding (i.e. application/x-www-form-urlencoded)
3. $_POST only when the data is in application/x-www-form-urlencoded Type can only be obtained when submitting.