PHP Receiving XML

889

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.

517

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:

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

or receive as a raw post data:

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

Answer

Solution:

CURLOPT_POSTFIELDS requires an array:

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

Then retrieve it like:

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

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

Source

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.