我提交POST到一个php页面如下:
{a:1}
这是请求的主体(POST请求)。 在php中,我需要做什么来提取这个值?
var_dump($_POST);
不是解决办法,行不通。
我提交POST到一个php页面如下:
{a:1}
这是请求的主体(POST请求)。 在php中,我需要做什么来提取这个值?
var_dump($_POST);
不是解决办法,行不通。
当前回答
检查$HTTP_RAW_POST_DATA变量
其他回答
在数组中返回值
$data = json_decode(file_get_contents('php://input'), true);
检查$HTTP_RAW_POST_DATA变量
如果你安装了pecl/http扩展,你也可以使用这个:
$request = new http\Env\Request();
$request->getBody();
http_get_request_body()根据文档http://php.net/manual/fa/function.http-get-request-body.php显式地用于获取PUT和POST请求的主体
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());