How to get body of a POST in php?

后端 未结 8 1814
小蘑菇
小蘑菇 2020-11-22 01:25

I submit as POST to a php page the following:

{a:1}

This is the body of the request (a POST request).
In php, what do I have to do to

8条回答
  •  名媛妹妹
    2020-11-22 01:55

    function getPost()
    {
        if(!empty($_POST))
        {
            // when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request
            // NOTE: if this is the case and $_POST is empty, check the variables_order in php.ini! - it must contain the letter P
            return $_POST;
        }
    
        // when using application/json as the HTTP Content-Type in the request 
        $post = json_decode(file_get_contents('php://input'), true);
        if(json_last_error() == JSON_ERROR_NONE)
        {
            return $post;
        }
    
        return [];
    }
    
    print_r(getPost());
    

提交回复
热议问题