PHP Receiving XML
I have a small PHP script that is listening for a POST request. I'm expecting xml, always. Normally I'm the guy sending the xml requests. But today I'm on the receiving side.
I figured it would be a simple case of listening for a $_POST, but I guess I may be incorrect - I'm getting nothing.
Here's my script that waits for anything xml:
<?php
if(isset($_POST)) {
mail("[email protected]","some title i want", print_r($_POST, true));
}else{
die("uh, what happened?");
}
?>
And here is a simple xml string I'm sending from another place:
<?php
$xml_data ='
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don\'t forget me this weekend!</body>
</note>
';
function sendXML2Server($URL,$XML){
$xml_data = trim($XML);
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
echo sendXML2Server('https://someurl.com/inboundxml.php',$xml_data)
?>
And here's what I get in my email:
Array ( )
I'm guessing I'm not working with an array correctly, but maybe there's something else I'm missing in all this. I'm expecting to get back the actual xml string.
Answer
Solution:
You sending nothing but data, thats why PHP cannot interpreter this data as some key&value. So, you need to send it as a value of variable:
or receive as a raw post data:
Answer
Solution:
CURLOPT_POSTFIELDS requires an array:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('content'=>$xml_data));
Then retrieve it like: