Rytis
Rytis

Reputation: 1445

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.

Upvotes: 1

Views: 1781

Answers (2)

Ole Haugset
Ole Haugset

Reputation: 3797

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.

Upvotes: 0

Trick
Trick

Reputation: 646

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.

Upvotes: 2

Related Questions