The differences between form POST and fsockopen submission are compared. $ _ POST and php: input can get the value when the form is submitted in POST mode. $ HTTP_RAW_POST_DATA is empty $ _ POST organizes the submitted data in the joined array mode and encodes it, such as urldecode and even encoding conversion. Php: the input stream can be used to obtain unprocessed PO through file reading. The difference between the form POST and fsockopen submission is different.
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.
Php: // input allows reading original POST data. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings. Php: // input cannot be used for enctype = "multipart/form-data ".
Fsockopen submits POST data
Example:
$ 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.0 \ r \ n ");
Fwrite ($ sock, "Host: localhost \ r \ n ");
Fwrite ($ sock, "Content-type: application/x-www-form-urlencoded \ r \ n ");
Fwrite ($ sock, "Content-length:". strlen ($ data). "\ r \ n ");
Fwrite ($ sock, "Accept: */* \ r \ n ");
Fwrite ($ sock, "\ r \ n ");
Fwrite ($ sock, "$ data \ r \ n ");
Fwrite ($ sock, "\ r \ n ");
$ Headers = "";
While ($ str = trim (fgets ($ sock, 4096 )))
$ Headers. = "$ str \ n ";
Echo "\ n ";
$ Body = "";
While (! Feof ($ sock ))
$ Body. = fgets ($ sock, 4096 );
Fclose ($ sock );
Echo $ body;
Consistent with (1) Results
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 is only available when data is submitted by application/x-www-form-urlencoded type.