Home >Backend Development >PHP Tutorial >PHP POST method_PHP tutorial
Compare the differences between form POST and fsockopen submission methods. When the form is submitted via POST $_POST and php://input can get the value, $HTTP_RAW_POST_DATA is empty 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". fsockopen submits POST data $sock = fsockopen("localhost", 80, $errno, $errstr, 30); Consistent with the result (1) Conclusion: 2. $HTTP_RAW_POST_DATA is only valid when the Content-Type type of POST is not recognized by PHP POST data usually submitted through page forms cannot be extracted through $HTTP_RAW_POST_DATA. 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. 3. $_POST can only be obtained when the data is submitted by application/x-www-form-urlencoded type.
$_POST organizes the submitted data in an associative array, and performs encoding processing on it, such as urldecode, and even encoding conversion.
php://input can obtain unprocessed POST raw data through file reading through the input stream
Example:
if (!$sock) die("$errstr ($errno)
");
$data = "txt=" . urlencode("中") . "&bar=" . urlencode("Value for Bar");
fwrite($sock, "POST /posttest/response. php HTTP/1.0
");
fwrite($sock, "Host: localhost
");
fwrite($sock, "Content-type: application/x-www-form-urlencoded
");
fwrite($sock, "Content-length: " . strlen($data) . "
");
fwrite($sock, "Accept: */*
");
fwrite($sock, "
");
fwrite($sock, "$data
");
fwrite($sock, "
");
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str
";
echo "
";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
echo $body;
1. Use php://input to easily get the original POST data
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)