javascript file_get_contents('php://input'); 使用 application/x-www-form-urlencoded;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25937775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:16:03  来源:igfitidea点击:

file_get_contents('php://input'); with application/x-www-form-urlencoded;

javascriptphpjquery

提问by Eric

I've read a few questions about the subject here on but couldn't find the answer I'm looking for. I'm doing some $.post with jQuery to a PHP5.6 server.

我在这里阅读了一些有关该主题的问题,但找不到我正在寻找的答案。我正在做一些 $.post 与 jQuery 到 PHP5.6 服务器。

$.post('/', {a:100, b:'test'}, function(data){
}, 'json');

The encoding from the console is

控制台的编码是

Content-Type    application/x-www-form-urlencoded; charset=UTF-8

If I try to read the POST data with a regular $_POST, PHP5.6 alerts me

如果我尝试使用常规 $_POST 读取 POST 数据,PHP5.6 会提醒我

PHP Deprecated:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead

So then I've tried the suggestion, added always_populate_raw_post_data = -1 in php.ini and

然后我尝试了这个建议,在 php.ini 中添加了 always_populate_raw_post_data = -1 和

json_decode(file_get_contents("php://input"));

PHP5.6 alerts me that it is invalid

PHP5.6 提示我无效

PHP Warning:  First parameter must either be an object or the name of an existing class

So I've dumped file_get_contents("php://input") and it's a string.

所以我转储了 file_get_contents("php://input") 并且它是一个字符串。

a=100&b="test"

So I've parsed the string and encoded then decoded

所以我解析了字符串并编码然后解码

parse_str(file_get_contents("php://input"), $data);             
$data = json_decode(json_encode($data));
var_dump($data);    

And THEN I finally have my $data as an object and not an array, as a true JSON object.

然后我终于把我的 $data 作为一个对象而不是一个数组,作为一个真正的 JSON 对象。

I've resorted to keep on using $_POST for now... But then I'm wondering about upgrading PHP..

我现在继续使用 $_POST ......但后来我想知道升级 PHP ..

The question here is that, is there a straighter forward solution to this or does it mean using file_get_contents("php://input") also means doing the parsing decoding encoding shenanigans?

这里的问题是,是否有更直接的解决方案,或者是否意味着使用 file_get_contents("php://input") 也意味着进行解析解码编码恶作剧?

Edit:so it appears this doesn't work either on multi levels json's. Consider the following:

编辑:所以看起来这在多级 json 上也不起作用。考虑以下:

{"a":100, "b":{"c":"test"}}

As sent in Ajax/Post

在 Ajax/Post 中发送

{a:100, b:{c:"test"}}

Doing

正在做

parse_str(file_get_contents("php://input"), $post);
var_dump($post);

Will output

会输出

array(2) {
 ["a"]=>string(8) "100"
 ["b"]=>string(16) "{"c":"test"}"
}

Or doing (as suggested)

或做(如建议)

parse_str(file_get_contents("php://input"), $post);
$post= (object)$post;

Will output

会输出

object(stdClass)#11 (2) {
 ["a"]=>string(8) "100"
 ["b"]=>string(16) "{"c":"test"}"

}

}

How do I transform file_get_contents("php://input") into a true object with the same "architecture" without using a recursive function?

如何在不使用递归函数的情况下将 file_get_contents("php://input") 转换为具有相同“架构”的真实对象?

Edit2 : My mistake, the suggested worked, I got side tracked in the comments with JSON.stringify which caused the error. Bottom line: it works with either json_decode(json_encode($post)) or $post=(object)$post;

Edit2 :我的错误,建议的工作,我在 JSON.stringify 的评论中被侧面跟踪,这导致了错误。底线:它适用于 json_decode(json_encode($post)) 或 $post=(object)$post;

To recap, using jQuery $.post :

回顾一下,使用 jQuery $.post :

$.post('/', {a:100, b:{c:'test'}}, function(data){
}, 'json');

parse_str(file_get_contents("php://input"), $data);             
$data = json_decode(json_encode($data));

or

或者

parse_str(file_get_contents("php://input"), $data);
$data= (object)$data;

No need to use JSON.stringify

无需使用 JSON.stringify

采纳答案by Eric

My mistake, the suggested worked, I got side tracked in the comments with JSON.stringifywhich caused the error. Bottom line: it works with either json_decode(json_encode($post)) or $post=(object)$post; The answer Michael gave is correct but side tracked me and I left the error in my code. JSON.stringify is only useful when posting the data from a form as I replied in the comment.

我的错误,建议的工作,我在 JSON.stringify 的评论中被忽略了,这导致了错误。底线:它适用于 json_decode(json_encode($post)) 或 $post=(object)$post; 迈克尔给出的答案是正确的,但我忽略了我,我把错误留在了我的代码中。JSON.stringify 仅在从我在评论中回复的表单中发布数据时才有用。

回答by Michael Berkowski

Receiving serialized/urlencoded POST data in the request's POST body as you are, you've correctly transformed it into an array with parse_str()already.

在请求的 POST 正文中接收序列化/urlencoded POST 数据,您已经正确地将其转换为数组parse_str()

However, the step of encoding then decoding JSON in order to transform that into the object(as opposed to array) you're looking for is unnecessary. Instead, PHP will happily cast an associative array into an object of class stdClass:

但是,编码然后解码 JSON 以将其转换为您正在寻找的对象(而不是数组)的步骤是不必要的。相反,PHP 会很乐意将关联数组转换为 class 的对象stdClass

parse_str(file_get_contents("php://input"), $data);
// Cast it to an object
$data = (object)$data;

var_dump($data);

A little more information is available in the PHP documentation on type casting.

有关类型转换的 PHP 文档中提供更多信息。

回答by georg

In order to send raw json data, you have to stop jQuery from url-encoding it:

为了发送原始 json 数据,您必须停止 jQuery 对其进行 url 编码:

    data = {"a":"test", "b":{"c":123}};

    $.ajax({
        type: 'POST',
        url:  '...',
        data: JSON.stringify(data), // I encode it myself
        processData: false          // please, jQuery, don't bother
    });

On the php side, just read php://inputand json_decodeit:

在PHP端,只是阅读php://inputjson_decode它:

$req = file_get_contents("php://input");
$req = json_decode($req);