admin管理员组文章数量:1417070
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', 80, $errno, $errstr, 5);
fputs($fp, "POST /test_out.php HTTP/1.1\r\n");
fputs($fp, "Host: www.yourpage\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 'e back with facts'. So here you go.
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', 80, $errno, $errstr, 5);
fputs($fp, "POST /test_out.php HTTP/1.1\r\n");
fputs($fp, "Host: www.yourpage\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 'e back with facts'. So here you go.
Share Improve this question edited Nov 21, 2011 at 15:30 PiTheNumber asked Nov 21, 2011 at 15:13 PiTheNumberPiTheNumber 23.6k17 gold badges113 silver badges189 bronze badges 3- 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 -- Not sure what your question is. Why do you want to omit the required header? – mario Commented Nov 21, 2011 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 Commented Nov 21, 2011 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 Commented May 31, 2012 at 21:14
1 Answer
Reset to default 8You 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.
本文标签: phpEmpty POST without ContenttypeStack Overflow
版权声明:本文标题:php - Empty $_POST without Content-type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745260287a2650325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论