Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在我的PHP程序中,URL编码似乎妨碍了正确的json编码/解码_Php_Json_Http_Urlencode_Simpletest - Fatal编程技术网

在我的PHP程序中,URL编码似乎妨碍了正确的json编码/解码

在我的PHP程序中,URL编码似乎妨碍了正确的json编码/解码,php,json,http,urlencode,simpletest,Php,Json,Http,Urlencode,Simpletest,我正在实现一个PHP脚本,它接收一条HTTP POST消息,消息体中有一个json字符串,与一个“report”参数相关联。所以HTTPPOST报告=。 我正在使用SimpleTest(PHP单元测试)对此进行测试 我构建json: $array = array("type" => "start"); // DEBUG $report = json_encode($array); 我发邮件: $this->post(LOCAL_URL, array("report"=>$js

我正在实现一个PHP脚本,它接收一条HTTP POST消息,消息体中有一个json字符串,与一个“report”参数相关联。所以HTTPPOST报告=。 我正在使用SimpleTest(PHP单元测试)对此进行测试

我构建json:

$array = array("type" => "start"); // DEBUG
$report = json_encode($array);
我发邮件:

$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
SimpleTest会自动选择“application/x-www-form-urlencoded”内容类型。当我将其设置为“application/json”时,我的PHP脚本没有看到任何参数,因此也找不到“report”变量。 我想url编码出了问题,但我不知道应该如何获得json访问

还有,这里的惯例是什么?即使只发送整个json正文,也会使用键/值方法吗?或者我可以将json字符串转储到HTTP POST的主体中,并以某种方式读取它吗?(在没有变量指向的情况下,我实际上没有成功地读出它)

无论如何,我希望问题能说得清楚一点。 先谢谢你


迪特尔(Dieter)

听起来你好像启用了魔法引号(这是一个很大的禁忌)。我建议您禁用此功能,否则,请通过stripslashes()运行所有输入


但是,最好将POST数据作为键/值对引用,否则必须读取php://input 流。

要获得快速修复,请尝试:

$report = stripslashes($_POST['report']);
更好。G=Get,P=Post,C=Cookie

在你的情况下。Post值会自动以单斜杠引用(“魔术”)


阅读。

问题确实在于神奇的引号,显然在我的MAMP版本中默认启用了它。如果我现在将其作为键/值对(json是值)发送,并且不定义内容类型,那么它就像一个符咒。但是,我不能使用content-type-application/json,但我想这是因为我违反了一些RFC之类的东西,这是我明确不想做的。感谢@johncartwright和@hakre的大力帮助!提示:选择其中一个答案作为“答案”,例如约翰的。@hakre:Done,谢谢提示:)