【问题标题】:POST requests to php only appearing in $_GET var对 php 的 POST 请求仅出现在 $_GET var
【发布时间】:2018-07-11 04:15:48
【问题描述】:

我将 POST 数据发送到一个 php 页面并通过var_dump($_POST) 接收它。但是,$_POST 始终为空,$_GET 包含这些值。我确定它是一个 POST 请求,并且已经在 Postman REST 客户端和 cURL 中进行了测试。我正在使用 PHP 5.5.3 和 Apache 2.2。 谢谢

编辑: 我将 php 脚本用作 api,因此目前我正在使用上述工具进行测试,以使用 POST 测试请求。 例如我正在使用这个 cURL 请求curl -X POST http://dev/project/build/api/registrants.php?hello=there

我的 $_SERVER 的内容(部分地区已编辑)

array(25) {
  ["HTTP_USER_AGENT"]=>
  string(11) "curl/7.37.1"
  ["HTTP_HOST"]=>
  string(3) "dev"
  ["HTTP_ACCEPT"]=>
  string(3) "*/*"
  ["PATH"]=>
  string(29) "/usr/bin:/bin:/usr/sbin:/sbin"
  ["SERVER_SIGNATURE"]=>
  string(0) ""
  ["SERVER_SOFTWARE"]=>
  string(67) "Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8za DAV/2 PHP/5.5.3"
  ["SERVER_NAME"]=>
  string(3) "dev"
  ["SERVER_ADDR"]=>
  string(9) "127.0.0.1"
  ["SERVER_PORT"]=>
  string(2) "80"
  ["REMOTE_ADDR"]=>
  string(9) "127.0.0.1"
  ["DOCUMENT_ROOT"]=>
  string(25) "/Applications/MAMP/htdocs"
  ["SERVER_ADMIN"]=>
  string(15) "you@example.com"
  ["SCRIPT_FILENAME"]=>
  string(59) "/Applications/MAMP/htdocs/project/build/api/registrants.php"
  ["REMOTE_PORT"]=>
  string(5) "51096"
  ["GATEWAY_INTERFACE"]=>
  string(7) "CGI/1.1"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["REQUEST_METHOD"]=>
  string(4) "POST"
  ["QUERY_STRING"]=>
  string(10) "hello=there"
  ["REQUEST_URI"]=>
  string(45) "/project/build/api/registrants.php?hello=there"
  ["SCRIPT_NAME"]=>
  string(34) "/project/build/api/registrants.php"
  ["PHP_SELF"]=>
  string(34) "/project/build/api/registrants.php"
  ["REQUEST_TIME_FLOAT"]=>
  float(1416256385.03)
  ["REQUEST_TIME"]=>
  int(1416256385)
  ["argv"]=>
  array(1) {
    [0]=>
    string(10) "hello=there"
  }
  ["argc"]=>
  int(1)
}

【问题讨论】:

  • 如果这是一个问题,需要查看一些代码
  • 问题是什么?我们将查看您的发送方式以帮助您解决此问题。
  • 客户端和服务器之间会不会有一些愚蠢的代理?
  • 正在发送发布请求,但您并未发送请求正文。
  • 取决于您的客户,但对于 curl:superuser.com/questions/149329/…

标签: php post


【解决方案1】:

在您的<form> 标记中,确保存在method="POST" 属性。

【讨论】:

  • 我使用 AJAX 来调用它而不是 html 表单
  • @rob。您需要发出POST 请求。没关系。
  • 遇到同样的问题,我最终将整个 API 更改为 GET 请求。
【解决方案2】:

好吧,我遇到了同样的问题,我发现错误在于我使用了错误的标头,并且变量应该在请求的正文中传递,而不是作为 URL 参数。我正在使用 Postman 生成 POST 请求,我得到了 $_GET 填充和 $_POST 作为 (array(0)),所以我读到了 $_POST 超全局并且它只有在使用 application/x-www-form-urlencoded 时才会被填充或 multipart/form-data 作为请求中的 HTTP Content-Type,根据此处的 PHP 文档:http://php.net/manual/en/reserved.variables.post.php。然后,我阅读了 Postman 手册,它在打印屏幕上显示参数必须在正文中,如会话请求正文中所述:https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests。正如史蒂夫对您的问题的评论所说,使用 curl 此请求的格式应如下所示:

curl -X POST --data "param1=value1&param2=value2" https://example.com/resource.cgi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2021-03-11
    相关资源
    最近更新 更多