PHP解码JSON POST [英] PHP decode JSON POST
本文介绍了PHP解码JSON POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以JSON形式接收POST
数据.我将其卷曲为:
I"m trying to receive POST
data in the form of JSON. I'm curling it as:
curl -v --header 'content-type:application/json' -X POST --data '{"content":"test content","friends":[\"38383\",\"38282\",\"38389\"],"newFriends":0,"expires":"5-20-2013","region":"35-28"}' http://testserver.com/wg/create.php?action=post
在PHP方面,我的代码是:
On the PHP side my code is:
$data = json_decode(file_get_contents('php://input'));
$content = $data->{'content'};
$friends = $data->{'friends'}; // JSON array of FB IDs
$newFriends = $data->{'newFriends'};
$expires = $data->{'expires'};
$region = $data->{'region'};
但是,即使我print_r ( $data)
,也什么也没有退还给我.这是处理不带表单的POST
的正确方法吗?
But even when I print_r ( $data)
nothing gets returned to me. Is this the right way of processing a POST
without a form?
推荐答案
您提交的JSON数据无效JSON.
The JSON data you're submitting is not valid JSON.
在外壳程序中使用'时,它不会像您怀疑的那样处理\.
When you use ' in your shell, it will not handle \" as you suspect.
curl -v --header 'content-type:application/json' -X POST --data '{"content":"test content","friends": ["38383","38282","38389"],"newFriends":0,"expires":"5-20-2013","region":"35-28"}'
按预期工作.
<?php
$foo = file_get_contents("php://input");
var_dump(json_decode($foo, true));
?>
输出:
array(5) {
["content"]=>
string(12) "test content"
["friends"]=>
array(3) {
[0]=>
string(5) "38383"
[1]=>
string(5) "38282"
[2]=>
string(5) "38389"
}
["newFriends"]=>
int(0)
["expires"]=>
string(9) "5-20-2013"
["region"]=>
string(5) "35-28"
}
这篇关于PHP解码JSON POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文