我将以下内容作为POST提交到php页面:
{a:1}
这是请求的主体( POST请求)。
在php中,我必须做什么才能提取这个值呢?
var_dump($_POST);
不是解决方案,不起作用。
发布于 2019-03-12 18:51:26
数组中的返回值
$data = json_decode(file_get_contents('php://input'), true);
发布于 2016-03-16 22:41:25
$_POST
为空的一个可能原因是请求不是POST
,或者不再是POST
……它可能是作为post开始的,但在某个地方遇到了301
或302
重定向,这被切换到了GET
!
检查$_SERVER['REQUEST_METHOD']
以检查是否为这种情况。
请参阅https://stackoverflow.com/a/19422232/109787,很好地讨论了为什么这种情况不应该发生但仍然发生。
发布于 2019-11-03 18:38:57
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());
https://stackoverflow.com/questions/8945879
复制相似问题