Reputation: 1969
I've tried all of them methods on could find that have worked for other folks, but with no luck.
Here's my PHP code:
parse_str(file_get_contents("php://input"), $put_data);
echo "foo is: " . $put_data['foo'] . "\n";
No matter what I try, I get 'foo is: '
I'm thinking this is specific to Apache as running the same script on an nginx server shows the expected 'foo is: bar'
Things I've tried with no change in the result:
Changing my Content-Type header to application/x-www-form-urlencoded.
Adding a LimitExcept directive to my apache config, like so (yes, I restarted after making the change)
<Directory "/MyApp/Directory/">
Allow From All
AllowOverride All
<LimitExcept GET POST PUT OPTIONS DELETE>
Deny from All
</LimitExcept>
</Directory>
Using stream_get_contents like so:
$put_data = fopen("php://input", "r");
$data = stream_get_contents($put_data);
echo "data is: " . $data;
fclose($put_data);
Here are the response headers:
Date: Wed, 11 Jun 2014 04:03:09 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.24
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 7
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Thanks to any who can offer help. Pulling what's left of my hair out on this one for hours now.
Upvotes: 3
Views: 2758
Reputation: 775
I've tested your code and it's working fine on my setup which is very similar to yours. The only thing I can think of is this:
Note: A stream opened with php://input can only be read once; the stream does not support seek operations.
From: http://www.php.net/manual/en/wrappers.php.php
Try and find a place in your code which is already reading 'php://input'. If you're using a framework of some sort, this would be my guess. I would search the entire project files for "php://input" to find out.
If that doesn't seem to be the case, please pastebin the results of the following:
print_r(file_get_contents("php://input"));exit;
var_dump(file_get_contents("php://input"));exit;
P.S. The rest of the note from the PHP manual website reads:
However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.
The SAPI implementation could be the difference between your nginx and Apache behaviours.
Upvotes: 1