2

Possible Duplicate:
Get raw post data

Reading from php://input works, except when the request body is in multipart/form-data format, in which case nothing is read.

I've heard that php://input can only be read from once, which is what I suspect may be causing this (i.e. PHP may be reading from php://input when it sees multipart/form-data before I can get my hands on it). However, in testing I've found that I am able to read from the stream multiple times without issue.

Is there a way to get the request body no matter what format?

Here's what I've got so far:

$body = '';   

$handle = fopen('php://input', 'r');
while(!feof($handle)) {
    $body .= fread($handle, 1024);
}
Community
  • 1
  • 1
hamburgerly
  • 161
  • 2
  • 5
  • You're right, thanks. Shame it requires such an ugly hack. – hamburgerly Nov 15 '12 at 00:39
  • 1
    If you are using PHP 5.4, you can try http://www.php.net/manual/en/ini.core.php#ini.enable-post-data-reading But then you have to make custom parsers for populating `$_POST` and `$_FILES` – Esailija Nov 15 '12 at 00:45

1 Answers1

0

You can make PHP always populate $HTTP_RAW_POST_DATA See http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data not the most efficient way of doing it though.

xelber
  • 4,197
  • 3
  • 25
  • 33
  • It says right there *$HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data".* – Esailija Nov 15 '12 at 00:52
  • $HTTP_RAW_POST_DATA is now deprecated. Please us 'php://input' – Tuesdave Jul 30 '15 at 21:00
  • The issue with using `php://input` is if one part of the script (a lib include, or sdk) already reads `php://input`, then any time later in a script execution that tries to do the same thing, fails. This is the most stupid depreciation of something useful, ever done to later php versions. – IncredibleHat Mar 04 '20 at 15:10