URL 编码似乎妨碍了我的 PHP 程序中正确的 json 编码/解码
我正在实现一个 PHP 脚本,该脚本接收 HTTP POST 消息,消息正文中有一个 json 字符串,与“report”参数绑定。所以 HTTP POST 报告=。 我正在使用 SimpleTest(PHP 单元测试)对此进行测试。
我构建 json:
$array = array("type" => "start"); // DEBUG
$report = json_encode($array);
我发送 POST:(
$this->post(LOCAL_URL, array("report"=>$json));
从 SimpleTest 调用 WebTestCase 类中的方法)。
SimpleTest 说它发送这个:
POST /Receiver/web/report.php HTTP/1.0
Host: localhost:8888
Connection: close
Content-Length: 37
Content-Type: application/x-www-form-urlencoded
report=%7B%22type%22%3A%22start%22%7D
我收到这样的:
$report = $_POST['report'];
$logger->debug("Content of the report parameter: $report");
$json = json_decode($report);
上面的调试语句给了我:
Content of the report parameter: {\"type\":\"start\"}
当我解码时,它给出了错误
Syntax error, malformed JSON
'application/x-www-form-urlencoded' 内容类型由 SimpleTest 自动选择。当我将其设置为“application/json”时,我的 PHP 脚本看不到任何参数,因此无法找到“report”变量。 我想 url 编码出了问题,但我不知道如何获取 json 。
另外,这里的通常做法是什么?即使您只发送整个 json 正文,是否也会使用键/值方法?或者我可以将 json 字符串转储到 HTTP POST 正文中并以某种方式将其读出吗? (在没有指向变量的情况下,我没有成功地实际读出它)。
无论如何,我希望这个问题能得到一些明确的说明。 提前谢谢大家。
迪特尔
I'm implementing a PHP script which receives an HTTP POST message with in the body a json string, tied to a 'report' parameter. So HTTP POST report=.
I'm testing this out with SimpleTest (PHP Unit Testing).
I build the json:
$array = array("type" => "start"); // DEBUG
$report = json_encode($array);
I send the POST:
$this->post(LOCAL_URL, array("report"=>$json));
(calls a method in the WebTestCase class from SimpleTest).
SimpleTest says it sends this:
POST /Receiver/web/report.php HTTP/1.0
Host: localhost:8888
Connection: close
Content-Length: 37
Content-Type: application/x-www-form-urlencoded
report=%7B%22type%22%3A%22start%22%7D
I receive as such:
$report = $_POST['report'];
$logger->debug("Content of the report parameter: $report");
$json = json_decode($report);
The debug statement above gives me:
Content of the report parameter: {\"type\":\"start\"}
And when I decode, it gives the error
Syntax error, malformed JSON
The 'application/x-www-form-urlencoded' content-type is automatically selected by SimpleTest. When I set it to 'application/json', my PHP script doesn't see any parameters and as such, can't find the 'report' variable.
I suppose something is going wrong with the url encoding, but I'm lost here as to how I should get the json accross.
Also, what is the usual practice here? Does one use the key/value approach even if you just send an entire json body? Or can I just dump the json string in the body of the HTTP POST and read it out somehow? (I had no success in actually reading it out without a variable to point to).
Anyway, I hope the problem is somewhat clearly stated.
Thanks a bunch in advance.
Dieter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你启用了魔术引号(这是一个很大的禁忌)。我建议您禁用此功能,否则,通过 stripslashes() 运行所有输入。
但是,最好将 POST 数据作为键/值对引用,否则您将必须读取 php://input 流。
It sounds like you have magic quotes enabled (which is a big no-no). I would suggest you disable this, otherwise, run all your input through stripslashes().
However, it is better practice to reference the POST data as a key/value pair, otherwise you will have to read the php://input stream.
要快速修复,请尝试:
更好的是,禁用魔术引号 GPC。 G=获取,P=发布,C=Cookie。
在你的情况下发布。发布值会自动(“神奇”)用单斜杠引用。
请阅读这里如何禁用魔术引号。
For the quick fix, try:
Better, disable magic quotes GPC. G=Get, P=Post, C=Cookie.
In your case Post. Post values get automatically ("magic") quoted with a single slash.
Read here how to disable magic quotes.