【问题标题】:PHP $_POST Not Returning ValuesPHP $_POST 不返回值
【发布时间】:2016-02-21 18:08:39
【问题描述】:

在 PHP 中,我有一个将数据提交给 PHP 脚本的表单。 PHP 脚本打印值,使用:

  $raw_post = file_get_contents('php://input');
  print_r($raw_post);
  print_r($_POST);
  print_r($_REQUEST);

所有这些都返回空/空数组,除了 $raw_post(又名 php://input)。

Chrome 的开发者工具还显示这些值已作为 POST 请求通过有效负载提交,状态为 200 OK,但 PHP 根本没有将它们设置为 $_POST 数组。

$raw_post 中的结果:

{"company_name":"test","primary_contact":"test","address":"test","function":"test","phone":"test","fax":"test","url":"test"}

$_POST 中的结果:

Array
(
)

$_REQUEST 中的结果:

Array
(
)

我无法找到此问题的解决方案...有人可以帮忙吗?

表单从 AngularJS 提交到 PHP 脚本。

新代码(网址编码):

app.factory('Companies', function($resource) {
    return $resource('/api.php/companies/:id', {id:''}, {
        'query': {method: 'GET', isArray: true},
        'view': {method: 'GET', isArray: true},
        'save': {
            method: 'POST',
            isArray: true,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}},
    });
});

【问题讨论】:

  • $raw_post 看起来像 JSON,php 不会为你解析它。如果你像一个 url 编码的字符串一样发送它。
  • 尝试通过 Postman Launcher 或类似插件手动触发您的 PHP 脚本。
  • Here 是你需要做的
  • 感谢 Halcyon,它现在可以用于设置数据类型。但如果可以的话,请参阅我对 Hanky 的回答的评论。

标签: php angularjs forms post


【解决方案1】:

哇,听起来很简单是不是

$raw_post = file_get_contents('php://input');
print_r($raw_post); 

这已经为您提供了 Posted JSON,只需对其进行解码 :)

$values=json_decode($raw_post,true);

现在,如果您想将所有这些数据存储回 $_POST,您可以这样做

$_POST=json_decode($raw_post,true);

这将为您提供发布的数据。

输出

Array
(
    [company_name] => test
    [primary_contact] => test
    [address] => test
    [function] => test
    [phone] => test
    [fax] => test
    [url] => test
)

Fiddle

【讨论】:

  • 向 OP 添加了一些新代码 - 数据现在通过 AngularJS 进行 url 编码,但是 $_POST 仍然是空的。我认为这是因为 chrome 说该方法仍然是 application/json,而数据现在是 url 编码的。有什么想法吗?
  • $raw_input 现在包含什么?
【解决方案2】:

这样试试,

print_r(json_decode($raw_post));

看起来你有 json 数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多