Page 1 of 1

[SOLVED] XML - Grabbing pure XML thru POST (not using xmlrpc

Posted: Mon Oct 18, 2004 6:28 am
by ihateevilbill
Hello all, lets see how you deal with this.

I have a php page that sits waiting for something to be sent to it (using POST)
Then basically echos out the POST as shown below:

Code: Select all

<?php
print_r($_POST);
?>
Now, when I use curl to send it some post data using the code below it echos out the array fine.

Code: Select all

<?php
$xmlvars='
<?xml version="1.0" ?>
<bht_fgsr>
 <depalc>GLA</depalc>
 <destalc>ALC</destalc>
 <outdate>23/10/04</outdate>
 <retdate>30/10/04</retdate>
 <adt>1</adt>
 <chd>0</chd>
 <inf>0</inf>
</bht_fgsr>';

$ch=curl_init("http://127.0.0.1/ws/catch.php");
curl_setopt($ch, CURL_HTTP_VERSION_1_1, true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,"xml=$xmlvars");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($ch);
echo $result;

?>
However, when i try and force this page into sending the request as pure XML as opposed to the form encoded 1 shown above by adding the line

curl_setopt ($ch,CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));

into the curl handler and changing

curl_setopt($ch,CURLOPT_POSTFIELDS,"xml=$xmlvars"); to
curl_setopt($ch,CURLOPT_POSTFIELDS,$xmlvars);
(ie not setting the posts variable name to xml so that it can be picked up on the next page as $_POST

Code: Select all

) it just brings back 'array' as the response.

Now, apparently in asp this is dead easy to pick up this post even although it doesnt hold a variable name.  Although the guy I originally asked had no idea how it could be done in php.  Can anyone help! PLEASE!!! lolz

PS I really dont want to have to use XMLRPC or NuSoap or any of the other stuff thats already out there.  It cant be that hard to grab this POST, surely  :? 

luv n kisses

Me.

Re: XML - Grabbing pure XML thru POST (not using xmlrpc)

Posted: Mon Oct 18, 2004 7:14 am
by Weirdan
ihateevilbill wrote: Now, apparently in asp this is dead easy to pick up this post even although it doesnt hold a variable name. Although the guy I originally asked had no idea how it could be done in php. Can anyone help! PLEASE!!! lolz
It's dead easy in php as well:

Code: Select all

function get_raw_post_data() {
    $fp = fopen("php://input", "r");
    $ret = fread($fp, $_SERVER['CONTENT_LENGTH']);
    fclose($fp);
    return $ret;
}
var_dump(get_raw_post_data());
:)

Posted: Mon Oct 18, 2004 7:20 am
by ihateevilbill
Did i ever tell you that 'u da man' ?
well I have now,

cheers again m8 :D

Posted: Mon Oct 18, 2004 11:28 am
by timvw
even shorter:

Code: Select all

$data = file_get_contents('php://input');

Posted: Tue Oct 19, 2004 5:56 am
by ihateevilbill
I love this place, cheers to both of you