tags:

views:

58

answers:

1

Hi All,

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

+1  A: 

You could use $HTTP_RAW_POST_DATA (manual entry) or the php://input stream.

From the manual:

php://input allows you to read raw POST 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 with enctype="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 "[email protected]" http://localhost/input.php

curl -F "[email protected];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 "[email protected];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 "[email protected]" -H "Content-Type: application/xml"

Tim Lytle
Hello Tim,The php://input will work with application/xml?Because is returning blank for me.I'm usind this code "file_get_contents("php://input");"Thanks
Flávio Leal
It _should work_, unless it's sent by the mentioned enctype (in which case some hackery could be done, but none that's really pleasant)
Wrikken
TIm, yes the xml is posted by the application/xml enctype :(Do you know what I can do in this case?
Flávio Leal
application/xml is not an enctype but a mimetype.
VolkerK
I've added some sample code and tests, perhaps more information on the code that's `POSTing` would be helpful.
Tim Lytle
Guys, I updated the post with more information on top
Flávio Leal
Still, one of those two ways should work. Have you tested `$HTTP_RAW_POST_DATA`?
Tim Lytle
Yes, I tested $HTTP_RAW_POST_DATA and returned nothing as well.Look I create a page here: http://www.xquad.com.br/messenger/test.phpThe code of this page is just: echo "php://input: ". file_get_contents('php://input') . "<br>"; echo("HTTP_RAW_POST_DATA: " . $HTTP_RAW_POST_DATA);Both are returning nothing, maybe can be a problem in my server?Best, Flavio
Flávio Leal
Just ran some cURL tests on you server and they work just fine. I get the echo'd input using `-d` or file using `-F` with `-H`. However, if I don't set a fake user agent (`-A "Mozilla/4.0"`) I get a `406` error for *any* request. That could be your problem.
Tim Lytle
But I can use the cURL without command line?I need to use the cURL on the PHP page.
Flávio Leal
I think you're missing the point of the cURL tests. I'm using cURL from the command line to (as much as possible) simulate the `POST` you describe. I'm trying to have cURL act like 'botplatform.com' because there's no way for me to observe a `POST` from their server. However, the results from the cURL test (getting a `406` error unless I fake a user agent), makes me think you're dealing with a server configuration issue.
Tim Lytle
Tim, do you have an ideia of what could by this issue? With that I can talk with my host.Best, Flavio
Flávio Leal