如何使用错误的标头获取POST参数

时间:2011-11-18 13:46:14

标签: php

我正在使用XDomainRequest从IE发出POST请求。 XDomainRequest没有在标题中发送Content-Type,它是not possible to set a custom header。我认为这会导致PHP不将post变量放在$_POST

如果我使用file_get_contents("php://input"),我可以看到参数已发送但我不确定如何在不破坏它们的情况下正确解析它们。有没有办法强制PHP收集POST参数?或者我如何安全地获取变量?

我正在使用this transport在jQuery中获取XDomainRequest并进行此调用:

jQuery.post('http://domain.de/io/login', 'email=test2@stuff.de&password=xxx', function(response)
{
    if (console) console.log(response);
    alert('response: ' + objectToString(response));
});

在PHP方面,我有:

print_r($_REQUEST, true);
echo file_get_contents("php://input");

适用于Firefox。但Firefox不使用传输。

2 个答案:

答案 0 :(得分:3)

@hakre:非常感谢。您的document描述了此处没有人相信的内容:

  

请求的主体应按照解释   Content-Type标题。

它还提到parse_str()完全符合我的需要。

这里我的代码填充$ _POST,如果它没有由PHP设置:

if (count($_POST) == 0)
{
    $request_body = file_get_contents("php://input");
    parse_str($request_body, $_POST);
}

我很乐意为此声名鹊起,但我不知道如何。无论如何你刚从我那里获得+50,所以我希望没问题。

答案 1 :(得分:-2)

正如我在jQuery.post()手册中看到的那样,data参数必须采用以下格式:

jQuery.post('http://domain.de/io/login', {email: "test2@stuff.de", password: "xxx"}, function(response)
{
    if (console) console.log(response);
    alert('response: ' + objectToString(response));
});