为了账号安全,请及时绑定邮箱和手机立即绑定

$_POST 在 nginx 上为空

$_POST 在 nginx 上为空

PHP
人到中年有点甜 2021-12-03 18:43:54
我的问题似乎与$_POST 为空类似。Nginx OSx, $_POST 在 nginx 中为空,$_GET 已填充但 $_REQUEST 为空,但是我找不到适合我的解决方案。长话短说,我无法将我的 POST 请求发送到server.php我的服务器上命名的文件。让我们假设 myserver.php只包含以下内容:<?php var_dump($_REQUEST);我的sites-available/domain.tld文件包含以下内容:server {    root /var/www/domain.tld/current/dist;    index index.html index.htm;    server_name domain.tld;    location / {        try_files $uri $uri/ index.php?$query_string =404;    }    location ~ \.php$ {            #limit_except POST {            #    allow 127.0.0.1;            #    deny  all;            #}            #if ( $request_method !~ ^POST$ ) {            #    return 405;            #}            include snippets/fastcgi-php.conf;            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;            #fastcgi_param REQUEST_METHOD $request_method;            fastcgi_param  REQUEST_METHOD     $echo_request_method;            fastcgi_param CONTENT_TYPE $content_type;            fastcgi_param CONTENT_LENGTH $content_length;            fastcgi_param REQUEST_BODY $request_body;            fastcgi_param QUERY_STRING $query_string;    }    location ~ /\.ht {            deny all;    }    listen 443 ssl; # managed by Certbot    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem; # managed by Certbot    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem; # managed by Certbot    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot}server {    server_name www.domain.tld;    return 301 $scheme://domain.tld$request_uri;}无论我提出什么“POST”请求,server.php都不会发送任何参数。输出server.php将是array(0) {}一些其他信息:请求是在请求 URL 上进行的: https://domain.tld/server.php请求方式: POST状态代码: 200 OK发送的 GET 请求https://domain.tld/server.php?foo=bar将向我显示发送的正确foo变量,其值为bar如果需要,我可以提供有关请求本身的详细信息。(axios在vue应用程序中使用)。请指教...
查看完整描述

1 回答

?
蛊毒传说

TA贡献1895条经验 获得超3个赞

这个答案可能有点晚了,但为了其他遇到同样问题的人:


问题原来是Content-Type标题有 value application/json。当 PHP 解析数据以放入$_POST超级全局时,它期望此标头具有值application/x-www-form-urlencodedor multipart/form-data,即 Web 标准内容类型。接收application/json内容类型数据时,您必须从php://input流中访问它。


解决方案最终看起来像这样:


$json_string_data = file_get_contents('php://input');

$decoded_data = json_decode($json_string_data, true);

如果您的应用程序希望数据在$_POST超级全局中可用,那么可以使用以下(无可否认的 hacky)解决方案:


$json_string_data = file_get_contents('php://input');

$decoded_data = json_decode($json_string_data, true);

$_POST = $decoded_data;

或者,为简洁起见:


$_POST = json_decode(file_get_contents('php://input'), true);


查看完整回答
反对 回复 2021-12-03
  • 1 回答
  • 0 关注
  • 299 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信