I have a PHP page that needs to read XML posted to this page. The post will be done by content-type: application/xml(not application/x-www-form-urlencoded).
For example, my page is called test.php, another server will post the XML to this page (test.php). My question is how can I create a code to this test.php page to read the xml posted to it?
UPDATE
First, thanks a lot for the answers and the time spent to help me. I´ll try to explaing better my problem. I´m creating a Messenger Robot. I´m using a plataform called "botplataform". More information here: http://code.google.com/p/botplatform/wiki/RobotServiceHostedOnWebServer
Basicaly this plataform works like this: I type a message on my instante messager (in this case MSN), the message that I typed is sent to my PHP page by a XML POST. The information of how this plataform do that is this:
"The post request content-type is application/xml(not application/x-www-form-urlencoded). So you should read the entire body as the xml data, you couldn't read any parameters."
My PHP file needs to "read" this XML to know what the user typep on MSN. And them I send back my answer to the botplataform that send the messege to the user.
I can send a answer to the botplataform, but I cant read the XML that the botplataform send to my php file.
If you want to test, just ad mainframe(at)xquad(dot)com(dot)br to your MSN an talk with it. It will only anser "Em manutencao" But it works, the php create a XML and echo it.
Thanks a lot, Flávio
You could use $HTTP_RAW_POST_DATA
(manual entry) or the php://input
stream.
From the manual:
php://input
allows you to read rawPOST
data. It is a less memory intensive alternative to$HTTP_RAW_POST_DATA
and does not need any special php.ini directives.php://input
is not available withenctype="multipart/form-data"
.
Update: Perhaps some examples may help.
Here's a simple script I'll call input.php
:
<?php
echo file_get_contents('php://input');
?>
Using cURL form the command line:
curl -d "<xml><stuff>test</stuff></xml>" http://localhost/input.php
And even setting the content type:
curl -H "Content-Type: application/xml" -d ...
The output is (as expected): <xml><stuff>test</stuff></xml>
If, however, you try to upload a file, which would normally set the enctype
to multipart/form-data
(note, not the content-type
) you'll get nothing.
Sample file (test.xml
):
<xml>
<stuff>test</stuff>
</xml>
Uploading file using cURL:
curl -F "name=@test.xml" http://localhost/input.php
curl -F "name=@test.xml;type=application/xml" http://localhost/input.php
Both will output nothing from input.php
. However, if you modify the script to look at the $_FILES
array (or however you would process normal uploads to PHP), you'll get the contents.
I'll call the new script form.php
:
<?php
echo file_get_contents($_FILES['name']['tmp_name']);
?>
A file upload with curl:
curl -F "name=@test.xml;type=application/xml" http://localhost/form.php
Will output the contents of test.xml
.
Between these two methods you should be able to handle the POST
file or data from the other server.
Second Update: Forcing application/xml
this way will stop $_FILES
from working, but then you're back to php://input
working again:
curl -F "name=@test.xml" -H "Content-Type: application/xml"