首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在php中获取帖子的正文?

如何在php中获取帖子的正文?
EN

Stack Overflow用户
提问于 2012-01-21 02:05:42
回答 8查看 352K关注 0票数 340

我将以下内容作为POST提交到php页面:

代码语言:javascript
复制
{a:1}

这是请求的主体( POST请求)。

在php中,我必须做什么才能提取这个值呢?

代码语言:javascript
复制
var_dump($_POST); 

不是解决方案,不起作用。

EN

回答 8

Stack Overflow用户

发布于 2019-03-12 18:51:26

数组中的返回值

代码语言:javascript
复制
 $data = json_decode(file_get_contents('php://input'), true);
票数 32
EN

Stack Overflow用户

发布于 2016-03-16 22:41:25

$_POST为空的一个可能原因是请求不是POST,或者不再是POST……它可能是作为post开始的,但在某个地方遇到了301302重定向,这被切换到了GET

检查$_SERVER['REQUEST_METHOD']以检查是否为这种情况。

请参阅https://stackoverflow.com/a/19422232/109787,很好地讨论了为什么这种情况不应该发生但仍然发生。

票数 17
EN

Stack Overflow用户

发布于 2019-11-03 18:38:57

代码语言:javascript
复制
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());
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8945879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档