的file_get_contents( 'PHP://输入');与application / x-www-form-urlencoded; [英] file_get_contents('php://input'); with application/x-www-form-urlencoded;

查看:73
本文介绍了的file_get_contents( 'PHP://输入');与application / x-www-form-urlencoded;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里已经阅读了一些关于这个主题的问题,但找不到我正在寻找的答案。
我正在使用jQuery向PHP5.6服务器做一些$ .post。

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('/', {a:100, b:'test'}, function(data){
}, 'json');

控制台的编码是

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

如果我尝试用普通的$ _POST,PHP5读取POST数据。 6提醒我

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

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

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

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

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

PHP5.6警告我它无效

PHP5.6 alerts me that it is invalid

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

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

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

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);    

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

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

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

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

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

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

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

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

在Ajax / Post中发送

As sent in Ajax/Post

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

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

将输出

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

或做(按照建议)

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

将输出

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

}

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

编辑2:我的错误,建议工作,我在JSON.stringify的评论中跟踪了导致错误。
底线:它适用于json_decode(json_encode($ post))或$ post =(object)$ 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));

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

无需使用JSON.stringify

推荐答案

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

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; 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.

这篇关于的file_get_contents( 'PHP://输入');与application / x-www-form-urlencoded;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆