我正在为另一位开发人员设置我的服务器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()
,但虽然它们没有错误,但它们没有显示任何内容。
感谢您的帮助。
答案 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,但是......
我以为你说你没有得到任何东西
只会为两种标准HTML表单编码方法填充
if(!isset($_POST['command']))
$_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)
此处有更多信息:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1