1

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.

Rytis
  • 1,447
  • 1
  • 19
  • 38

2 Answers2

2

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: https://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.

Community
  • 1
  • 1
Trick
  • 636
  • 4
  • 7
0

You can check and see if there is any information stored in your post by doing:

print_r( $_POST );

What you would like to do might be sending the XML data in a key named xml or something like that resulting in $_POST['xml'] = 'your xml code'

EDIT:

It would also be nice if you could post the code you use to create the $_POST.

SharpC
  • 6,974
  • 4
  • 45
  • 40
Ole Haugset
  • 3,709
  • 3
  • 23
  • 44