This article describes several php post data acquisition techniques. For an experienced (1) form POST submission, PHP can obtain POST data $ _ POST and php: input values, $ HTTP_RAW_POST_DATA is empty $ _ POST is associated with an array. for an experienced
(1) PHP obtains POST data when the form POST method is submitted
$ _ POST and php: // input can get the value. $ HTTP_RAW_POST_DATA is empty.
$ _ POST organizes submitted data in an associated array and performs encoding, such as urldecode or even encoding conversion.
Php: // input: raw POST data that has not been processed can be obtained by reading files from the input stream.
(2) fsockopen submits POST data and PHP obtains POST data
- $ Sock = fsockopen ("localhost", 80,
$ Errno, $ errstr, 30 );
- If (! $ Sock) die ("$ errstr ($ errno) n ");
- $ Data = "txt =". urlencode ("Medium ").
"& Bar =". urlencode ("Value for Bar ");
- Fwrite ($ sock, "POST/posttest/response
. Php HTTP/1.0rn ");
- Fwrite ($ sock, "Host: localhostrn ");
- Fwrite ($ sock, "Content-type: applicat
Ion/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;
PHP obtains POST data. conclusion:
1. you can use php: // input to conveniently retrieve the original POST data.
2. $ HTTP_RAW_POST_DATA is valid only when the POST Content-Type is not recognized by PHP.
For example, the POST data submitted through the page form cannot be extracted through $ HTTP_RAW_POST_DATA. The encoding type attribute (enctype attribute) is application/x-www-form-urlencoded and multipart/form-data.
Note: Even if the enctype attribute is explicitly changed to an unrecognized PHP type on the page, it still does not work. Because the form submission encoding attribute is a form limitation, unrecognized types are considered to be submitted in the default encoding mode (application/x-www-form-urlencoded)
3. $ _ POST: PHP can obtain POST data only when data is submitted by application/x-www-form-urlencoded type.
(1) when submitting a form POST request, PHP can obtain POST data $ _ POST and php: // input, $ HTTP_RAW_POST_DATA is empty $ _ POST is connected to an array...