PHP “php://input” vs $_POST

前端 未结 6 1924
离开以前
离开以前 2020-11-21 23:55

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

相关标签:
6条回答
  • 2020-11-22 00:15

    php://input can give you the raw bytes of the data. This is useful if the POSTed data is a JSON encoded structure, which is often the case for an AJAX POST request.

    Here's a function to do just that:

      /**
       * Returns the JSON encoded POST data, if any, as an object.
       * 
       * @return Object|null
       */
      private function retrieveJsonPostData()
      {
        // get the raw POST data
        $rawData = file_get_contents("php://input");
    
        // this returns null if not valid json
        return json_decode($rawData);
      }
    

    The $_POST array is more useful when you're handling key-value data from a form, submitted by a traditional POST. This only works if the POSTed data is in a recognised format, usually application/x-www-form-urlencoded (see http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4 for details).

    0 讨论(0)
  • 2020-11-22 00:18

    If post data is malformed, $_POST will not contain anything. Yet, php://input will have the malformed string.

    For example there is some ajax applications, that do not form correct post key-value sequence for uploading a file, and just dump all the file as post data, without variable names or anything. $_POST will be empty, $_FILES empty also, and php://input will contain exact file, written as a string.

    0 讨论(0)
  • 2020-11-22 00:20

    So I wrote a function that would get the POST data from the php://input stream.

    So the challenge here was switching to PUT, DELETE OR PATCH request method, and still obtain the post data that was sent with that request.

    I'm sharing this maybe for someone with a similar challenge. The function below is what I came up with and it works. I hope it helps!

        /**
         * @method Post getPostData
         * @return array
         * 
         * Convert Content-Disposition to a post data
         */
        function getPostData() : array
        {
            // @var string $input
            $input = file_get_contents('php://input');
    
            // continue if $_POST is empty
            if (strlen($input) > 0 && count($_POST) == 0 || count($_POST) > 0) :
    
                $postsize = "---".sha1(strlen($input))."---";
    
                preg_match_all('/([-]{2,})([^\s]+)[\n|\s]{0,}/', $input, $match);
    
                // update input
                if (count($match) > 0) $input = preg_replace('/([-]{2,})([^\s]+)[\n|\s]{0,}/', '', $input);
    
                // extract the content-disposition
                preg_match_all("/(Content-Disposition: form-data; name=)+(.*)/m", $input, $matches);
    
                // let's get the keys
                if (count($matches) > 0 && count($matches[0]) > 0)
                {
                    $keys = $matches[2];
    
                    foreach ($keys as $index => $key) :
                        $key = trim($key);
                        $key = preg_replace('/^["]/','',$key);
                        $key = preg_replace('/["]$/','',$key);
                        $key = preg_replace('/[\s]/','',$key);
                        $keys[$index] = $key;
                    endforeach;
    
                    $input = preg_replace("/(Content-Disposition: form-data; name=)+(.*)/m", $postsize, $input);
    
                    $input = preg_replace("/(Content-Length: )+([^\n]+)/im", '', $input);
    
                    // now let's get key value
                    $inputArr = explode($postsize, $input);
    
                    // @var array $values
                    $values = [];
    
                    foreach ($inputArr as $index => $val) :
                        $val = preg_replace('/[\n]/','',$val);
    
                        if (preg_match('/[\S]/', $val)) $values[$index] = trim($val);
    
                    endforeach;
    
                    // now combine the key to the values
                    $post = [];
    
                    // @var array $value
                    $value = [];
    
                    // update value
                    foreach ($values as $i => $val) $value[] = $val;
    
                    // push to post
                    foreach ($keys as $x => $key) $post[$key] = isset($value[$x]) ? $value[$x] : '';
    
                    if (is_array($post)) :
    
                        $newPost = [];
    
                        foreach ($post as $key => $val) :
    
                            if (preg_match('/[\[]/', $key)) :
    
                                $k = substr($key, 0, strpos($key, '['));
                                $child = substr($key, strpos($key, '['));
                                $child = preg_replace('/[\[|\]]/','', $child);
                                $newPost[$k][$child] = $val;
    
                            else:
    
                                $newPost[$key] = $val;
    
                            endif;
    
                        endforeach;
    
                        $_POST = count($newPost) > 0 ? $newPost : $post;
    
                    endif;
                }
    
            endif;
    
            // return post array
            return $_POST;
        }
    
    0 讨论(0)
  • 2020-11-22 00:21

    The reason is that php://input returns all the raw data after the HTTP-headers of the request, regardless of the content type.

    The PHP superglobal $_POST, only is supposed to wrap data that is either

    • application/x-www-form-urlencoded (standard content type for simple form-posts) or
    • multipart/form-data (mostly used for file uploads)

    This is because these are the only content types that must be supported by user agents. So the server and PHP traditionally don't expect to receive any other content type (which doesn't mean they couldn't).

    So, if you simply POST a good old HTML form, the request looks something like this:

    POST /page.php HTTP/1.1
    
    key1=value1&key2=value2&key3=value3
    

    But if you are working with Ajax a lot, this probaby also includes exchanging more complex data with types (string, int, bool) and structures (arrays, objects), so in most cases JSON is the best choice. But a request with a JSON-payload would look something like this:

    POST /page.php HTTP/1.1
    
    {"key1":"value1","key2":"value2","key3":"value3"}
    

    The content would now be application/json (or at least none of the above mentioned), so PHP's $_POST-wrapper doesn't know how to handle that (yet).

    The data is still there, you just can't access it through the wrapper. So you need to fetch it yourself in raw format with file_get_contents('php://input') (as long as it's not multipart/form-data-encoded).

    This is also how you would access XML-data or any other non-standard content type.

    0 讨论(0)
  • 2020-11-22 00:27

    Simple example of how to use it

     <?php  
         if(!isset($_POST) || empty($_POST)) { 
         ?> 
            <form name="form1" method="post" action=""> 
              <input type="text" name="textfield"><br /> 
              <input type="submit" name="Submit" value="submit"> 
            </form> 
       <?php  
            } else { 
            $example = file_get_contents("php://input");
            echo $example;  }  
       ?>
    
    0 讨论(0)
  • 2020-11-22 00:29

    First, a basic truth about PHP.

    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.

    However, then you learn more ...

    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:

    1. application/x-www-form-urlencoded, and
    2. multipart/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.

    URL encoding? What the heck!!!???

    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.

    How in touch are you with JavaScript? :-)

    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, what gives?

    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).

    So many media types (MIME types in the past)

    XML, JSON, and YAML all have media types that can be put into an HTTP Content-Type header.

    • application/xml
    • applicaiton/json
    • application/yaml (although IANA has no official designation listed)

    Look how many media-types (formerly, MIME types) are defined by IANA.

    Look how many HTTP headers there are.

    php://input or bust

    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.

    1. Determine if the correct HTTP method has been indicated (GET, POST, PUT, PATCH, DELETE, ...)
    2. Determine if the HTTP Content-Type header has been transmitted.
    3. Determine if the value for the Content-Type is the desired media type.
    4. Determine if the data sent is well formed XML / JSON / YMAL / etc.
    5. If necessary, convert the data to a PHP datatype: array or object.
    6. If any of these basic checks or conversions fails, throw an exception!

    What about the character encoding?

    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?

    Two critical problems.

    1. You do not know how much data is coming through php://input.
    2. You do not know for certain the current encoding of the data stream.

    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:

    1. Stream size detection algorithm.
    2. Application defined stream size limits (Apache / Nginx / PHP limits may be too broad).

    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:

    1. Stream encoding detection algorithm.
    2. Dynamic / runtime stream filter definition algorithm (because you cannot know the starting encoding a priori).

    (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
    

    Therefore, the general steps might be ...

    Part I: HTTP Request Related

    1. Determine if the correct HTTP method has been indicated (GET, POST, PUT, PATCH, DELETE, ...)
    2. Determine if the HTTP Content-Type header has been transmitted.
    3. Determine if the value for the Content-Type is the desired media type.

    Part II: Stream Data Related

    1. Determine the size of the input stream (optional, but recommended).
    2. Determine the encoding of the input stream.
    3. If necessary, convert the input stream to the desired character encoding (UTF-8).
    4. If necessary, reverse any application level compression or encryption, and then repeat steps 4, 5, and 6.

    Part III: Data Type Related

    1. Determine if the data sent is well formed XML / JSON / YMAL / etc.

    (Remember, the data can still be a URL encoded string which you must then parse and URL decode).

    1. If necessary, convert the data to a PHP datatype: array or object.

    Part IV: Data Value Related

    1. Filter input data.

    2. Validate input data.

    Now do you see?

    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.

    0 讨论(0)
提交回复
热议问题