I have been directed to use the method php://input
instead of $_POST
when interacting with Ajax requests from JQuery. What I do not understand is t
PHP was not designed to explicitly give you a pure REST (GET, POST, PUT, PATCH, DELETE) like interface for handling HTTP requests.
However, the $_POST
, $_GET
, and $_FILES
superglobals, and the function filter_input_array() are very useful for the average person's / layman's needs.
The number one hidden advantage of $_POST
(and $_GET
) is that your input data is urldecoded automatically by PHP. You never even think about having to do it, especially for query string parameters within a standard GET request.
That being said, as you advance in your programming knowledge and want to use JavaScript's XmlHttpRequest
object (jQuery for some), you come to see the limitation of this scheme.
$_POST
limits you to the use of two media types in the HTTP Content-Type
header:
application/x-www-form-urlencoded
, andmultipart/form-data
Thus, if you want to send data values to PHP on the server, and have it show up in the $_POST
superglobal, then you must urlencode it on the client-side and send said data as key/value pairs--an inconvenient step for novices (especially when trying to figure out if different parts of the URL require different forms of urlencoding: normal, raw, etc..).
For all you jQuery users, the $.ajax()
method is converting your JSON to URL encoded key/value pairs before transmitting them to the server. You can override this behavior by setting processData: false
. Just read the $.ajax() documentation, and don't forget to send the correct media type in the Content-Type header.
Typically, if you are doing a normal, synchronous (when the entire page redraws) HTTP requests with an HTML form, the user-agent (web browser) will urlencode your form data for you. If you want to do an asynchronous HTTP requests using the XmlHttpRequest
object, then you must fashion a urlencoded string and send it, if you want that data to show up in the $_POST
superglobal.
Converting from a JavaScript array or object to a urlencoded string bothers many developers (even with new APIs like Form Data). They would much rather just be able to send JSON, and it would be more efficient for the client code to do so.
Remember (wink, wink), the average web developer does not learn to use the XmlHttpRequest
object directly, global functions, string functions, array functions, and regular expressions like you and I ;-). Urlencoding for them is a nightmare. ;-)
PHP's lack of intuitive XML and JSON handling turns many people off. You would think it would be part of PHP by now (sigh).
XML, JSON, and YAML all have media types that can be put into an HTTP Content-Type
header.
Look how many media-types (formerly, MIME types) are defined by IANA.
Look how many HTTP headers there are.
Using the php://input stream allows you to circumvent the baby-sitting / hand holding level of abstraction that PHP has forced on the world. :-) With great power comes great responsibility!
Now, before you deal with data values streamed through php://input
, you should / must do a few things.
AH, HA! Yes, you might want the data stream being sent into your application to be UTF-8 encoded, but how can you know if it is or not?
php://input
.Are you going to attempt to handle stream data without knowing how much is there first? That is a terrible idea. You cannot rely exclusively on the HTTP Content-Length
header for guidance on the size of streamed input because it can be spoofed.
You are going to need a:
Are you going to attempt to convert stream data to UTF-8 without knowing the current encoding of the stream? How? The iconv stream filter (iconv stream filter example) seems to want a starting and ending encoding, like this.
'convert.iconv.ISO-8859-1/UTF-8'
Thus, if you are conscientious, you will need:
(Update: 'convert.iconv.UTF-8/UTF-8'
will force everything to UTF-8, but you still have to account for characters that the iconv library might not know how to translate. In other words, you have to some how define what action to take when a character cannot be translated: 1) Insert a dummy character, 2) Fail / throw and exception).
You cannot rely exclusively on the HTTP Content-Encoding
header, as this might indicate something like compression as in the following. This is not what you want to make a decision off of in regards to iconv.
Content-Encoding: gzip
Part I: HTTP Request Related
Part II: Stream Data Related
Part III: Data Type Related
(Remember, the data can still be a URL encoded string which you must then parse and URL decode).
Part IV: Data Value Related
Filter input data.
Validate input data.
The $_POST
superglobal, along with php.ini settings for limits on input, are simpler for the layman. However, dealing with character encoding is much more intuitive and efficient when using streams because there is no need to loop through superglobals (or arrays, generally) to check input values for the proper encoding.