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:

if(isset($_POST)) {
    mail("[email protected]","some title i want", print_r($_POST, true)); 
    die("uh, what happened?");

And here is a simple xml string I'm sending from another place:

$xml_data ='
<body>Don\'t forget me this weekend!</body>

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);

    return $output;

echo sendXML2Server('',$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.




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:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml_data' => $xml_data));

or receive as a raw post data:

if(isset($HTTP_RAW_POST_DATA)) {
    mail("[email protected]","some title i want", print_r($HTTP_RAW_POST_DATA, true)); 
    die("uh, what happened?");



CURLOPT_POSTFIELDS requires an array:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('content'=>$xml_data));

Then retrieve it like:

if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['content'])) {
    mail("[email protected]","some title i want", print_r($_POST['content'], true)); 
    die("uh, what happened?");

People are also looking for solutions to the problem: php - How to send a request on facebook?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.