无法读取原始POST正文数据 - 在此示例中如何执行此操作?

时间:2013-12-13 04:22:23

标签: php http http-post

我正在为另一位开发人员设置我的服务器API。我目前正在使用Flash AIR将POST数据发送到我的服务器,并简单地提取变量,如

$command = $_POST['command'].

但是,他没有使用Flash,并且正在发送这样的数据:

https://www.mysite.com POST /api/account.php?command=login HTTP/1.1
Content-Type: application/json
Connection: close

command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}

我的服务器正在向他返回错误,指出缺少'command'参数。

我需要做什么才能从上面的数据中提取$ _POST var'命令'?

我已尝试file_get_contents('php://input')http_get_request_body(),但虽然它们没有错误,但它们没有显示任何内容。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

请求声称它正在发送JSON。

  

Content-Type:application / json

但是,这个:

command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}

...不是JSON。

如果您在{之前删除了所有内容,那么它将是JSON,您应该能够使用file_get_contents('php://input')阅读它(然后可以通过a decoder传递它。

  

我已尝试file_get_contents('php://input')http_get_request_body() ...他们没有显示任何内容。

他们应该工作。

  

当我为通信打印file_get_contents('php://input')时......我得到了command = login,但是......

我以为你说你没有得到任何东西

  

if(!isset($_POST['command']))

只会为两种标准HTML表单编码方法填充

$_POST。如果您正在使用JSON,那么它将不会被自动解析,您必须自己完成(使用有效的JSON输入(因此需要使用其余数据在JSON文本中编码其他数据)),{{ 1}}和file_get_contents('php://input'))。

答案 1 :(得分:1)

命令参数需要是数据的一部分,整个事情应该是有效的JSON。就像command=login一样,它是无效的JSON。

将其添加到params对象或制作包含对象,例如

{
 command:'login',
 params :{"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
}

答案 2 :(得分:1)

来自@Cole的“内容类型应为www-form-urlencoded”(正确答案)

此处有更多信息:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1