Rytis Rytis - 18 days ago 6
PHP Question

Reading non key/value POST data from PHP

I have a system that POSTs data to an URL without sending key/value pairs but instead using an XML payload. I need to read that XML data using PHP under Apache, but PHP's

$_POST
array is for key/value pairs and thus is blank as no keys are provided.

I tried reading from
php://input
and
php://stdin
, but that's also blank.

How can I read the raw POST data using PHP? I cannot control the input as it is being generated by a third-party application. Imagine this being a RESTful URL that accepts XML payload.

Answer

According to the PHP manual, php://input is not available with enctype="multipart/form-data".. If this is what's being POSTed to you, PHP will not allow you to access the raw data. One workaround to this issue:
(source: http://stackoverflow.com/questions/1361673/get-raw-post-data)

Add this to your apache.conf:

<Location "/backend/XXX.php">
        SetEnvIf Content-Type ^(multipart/form-data)(.*) MULTIPART_CTYPE=$1$2
        RequestHeader set Content-Type application/x-httpd-php env=MULTIPART_CTYPE
        RequestHeader set X-Real-Content-Type %{MULTIPART_CTYPE}e env=MULTIPART_CTYPE
</Location>

As of PHP 5.4, there is also the "enable-post-data-reading" ini setting (http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading).

From the PHP manual: "Disabling this option causes $_POST and $_FILES not to be populated. The only way to read postdata will then be through the php://input stream wrapper. This can be useful to proxy requests or to process the POST data in a memory efficient fashion."

Try disabling this setting and then reading from php://input.