0

我有一个返回每个请求的请求参数的函数:

    private function GetRequestParams() {
    $method = $_SERVER['REQUEST_METHOD'];
    switch (strtolower($method)) {
        case 'put':
            $this->requestParams = //parse_str(HttpResponse::getRequestBody())
            $this->requestParams = array_map('urldecode', $this->requestParams);
            break;
        case 'post':
            $this->requestParams = $_REQUEST;
            break;
        case 'get':
            $this->requestParams = $_GET;
            break;
        case 'delete':
            $this->requestParams = $_REQUEST;
            break;
        default:
            $this->requestParams = $_REQUEST;
    }
}

但是当我使用 GET 和 POST 调用相同的 url 时,$_POST 参数为空。我使用来自 XAMPP 工具的 WizTools RestClient 和 Apache Server 来调用以下 url: http://localhost:80/project/?item=1

对于 GET,请求参数正确包含“项目”,但对于 POST,请求参数为空。

似乎 post 方法被正确检测为以下函数,正确发送到 postDescription() 方法:

$method = strtolower($_SERVER['REQUEST_METHOD']) . 'Description';

我找到了将 php.ini post_max_size = 8* M * 编辑为 8* MB * 的信息,但这对我不起作用。

4

4 回答 4

6

$_GET填充有来自 URL 的查询字符串的数据。

$_POST填充了来自发布消息正文的数据。

如果您发出post请求,但在查询字符串中传递数据,则数据将出现在$_GETnot中$_POST

于 2012-06-15T19:18:11.467 回答
1

$_POST 由 HTML 表单填充。如果您有一个表单并使用 method="POST",那么表单的结果将放在 POST 中。否则,如果您使用表单中的 method="get" 或使用查询字符串(例如,index.php?foo=bar&this=that),那么结果将在 $_GET 中。

但是,使用 $_REQUEST 通常是安全的。

于 2012-06-15T23:15:18.110 回答
0

首先,您不应该像某些评论中提到的那样使用 $_REQUEST。

来自http://php.net/manual/en/reserved.variables.request.php

Note: 
The variables in $_REQUEST are provided to the script via the GET, POST,
and COOKIE input mechanisms and therefore could be modified by the remote user
and cannot be trusted.
The presence and order of variables listed in this array is defined according
to the PHP variables_order configuration directive.

正如昆汀在他的回答中已经说过的那样,如果您发出 POST 请求,则必须在正文中提交 POST-Data

来自维基百科http://en.wikipedia.org/wiki/POST_(HTTP )

姓名=Jonathan+Doe&年龄=23&公式=a+%2B+b+%3D%3D+13%25%21

问候

于 2012-06-15T19:26:48.183 回答
0

我不太明白您为什么要从一开始就检查两者,而是$_GET检索在 URL 中发送的参数(这就是您拥有它的方式),而$_POST检索“发布”到服务器的数据……通常是通过表单某种。

你的最终目标是什么?

于 2012-06-15T19:21:25.593 回答