1

I am making a POST request from IE using XDomainRequest. XDomainRequest does not sent a Content-Type in his header and it is not possible to set a custom header. I think this causes PHP to not place the post variables in $_POST.

I can see the parameters are send if I use file_get_contents("php://input") but I am not sure how to parse them correctly without breaking them. Is there a way to force PHP to collect the POST parameters? Or how can I get the variables safely?

I am using this transport to get XDomainRequest in jQuery and make this call:

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

On the PHP side I have:

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

Works fine with Firefox. But Firefox does not use the transport.

Community
  • 1
  • 1
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • 5
    POST variables don't magically disappear, not even if some headers are or are not present. Please show the code how you issue the request. – CodeCaster Nov 18 '11 at 13:48
  • 1
    `I think this causes PHP to not place the post variables in $_POST.` This false assertion is your weak link. Come back with facts! :) – Lightness Races in Orbit Nov 18 '11 at 13:49
  • It's not easy to cut out some example code. But how is it possible that php://input contains the variables and $_REQUEST does not? The only difference I can see is the request header I get from `getallheaders()` – PiTheNumber Nov 18 '11 at 13:56
  • 2
    Please see [Advanced handling of HTTP requests in PHP](https://gist.github.com/1028251) and http://php.net/manual/en/wrappers.php.php – hakre Nov 18 '11 at 14:01
  • @CodeCaster: See code above. The variables did not disappear. They just not in $_POST. – PiTheNumber Nov 18 '11 at 14:06

2 Answers2

3

@hakre: Thanks a lot. Your document describes what no one here belives:

The body of the request is to be interpreted according to the Content-Type header.

It also mentions parse_str() that does exactly what I needed.

Here my code to fill $_POST if it was not set by PHP:

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

I would love to give you reputation for this but I don't know how. Anyway you just got +50 from me, so I hope that will be ok.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
-2

As I can see in the jQuery.post() manual, the data parameter has to be in the following format:

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