5

If I make a POST request where the content-type is not set in the request header, the variable $_POST remains empty.

Question: How can I force PHP to fill $_POST?

I encountered the problem making an Javascript AJAX request using XDomainRequest where you can not define any headers. Just to make it easier for you to understand the problem you can simulate the same effect without Javascript in PHP this way:

$data = 'test=1';

$fp = fsockopen('www.yourpage.org', 80, $errno, $errstr, 5);
fputs($fp, "POST /test_out.php HTTP/1.1\r\n");
fputs($fp, "Host: www.yourpage.org\r\n");
//fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);

while(!feof($fp)) { echo fgets($fp, 128); }
fclose($fp);

test_out.php would be

var_dump($_POST);

Without the correct content-type the variables in $_POST magically disappear.

This is more an educational question. I am asking this because most people here can not imaging that the content-type has this effect. I was asked to 'come back with facts'. So here you go.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • There's nothing magical about it. That's how PHP works (and any other CGI interpreter for that matter). See [php-src/main/rfc1867.c](http://svn.php.net/viewvc/php/php-src/trunk/main/rfc1867.c?revision=316373&view=markup) -- Not sure what your question is. Why do you want to omit the required header? – mario Nov 21 '11 at 15:24
  • I don't want to. XDomainRequest does that and there is no way around it because you can not add custom headers. I marked the question with "Question:". – PiTheNumber Nov 21 '11 at 15:29
  • 1
    Awesome. Working on a php rest service. Suddenly $_POST disappeared. Yep. No content type. Took forever to figure out how to get $_POST back if we accept unique content types. Many thanks. – Eric G May 31 '12 at 21:14

1 Answers1

8

You can override/inject the necessary header using Apache mod_headers if you have to. Or otherwise resort to manually reconstructing the $_POST array. (See also userland multipart/form-data handler)

If it's always urlencoded, simply read from php://input (Which contains the raw POST request body) and use parse_str:

parse_str(file_get_contents("php://input"), $_POST);

That should recreate the POST array, pretty much like PHP would do.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • 2
    Greate, perfect answer! Last time I asked the [same question](http://stackoverflow.com/questions/8183397/how-to-get-post-parameters-with-wrong-header) I got -3 votes and +6 votes on wrong comments then they closed the question. Only one person (hakre) pointed in the right direction. I think it is sad when questions are closed because of a lack of knowledge. – PiTheNumber Nov 21 '11 at 15:52
  • Hmmm, okay. Small formulation issues often tick people off. -- But why did you ask it twice? It seems you already knew about the appropriate workaround. – mario Nov 21 '11 at 15:55
  • 4
    Because I say many people didn't know about this and I wanted a not closed question about this. I learn a lot reading questions and I hope so do others. Also I think it was not fair how my other question was handled. – PiTheNumber Nov 21 '11 at 16:05