PHP “php://input” vs $_POST

前端 未结 6 1925
离开以前
离开以前 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: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;
        }
    

提交回复
热议问题