1

I've been working on an API for receiving a XML file which in turn gonna be read in our databse. The parsing and all worked with local XML files. Now the last step was to check if the receiving of the XML was working and there is where I hit a roadblock. Been trying a lot of different things up untill now, but can't seem to get it to work. We would prefer to work with the REST method, but if it's really not gonna work we're willing to switch it up. Seen a lot of questions for sending or receiving a XML file, but never in combination. So hope I can get a solid answer on this and didn't miss a question that is a duplicate.

TL;DR: Sending and receiving XML files through PHP, don't know where I'm making a error The sending and receiving parts are in different scripts.

//Sending XML script.
    $ch = curl_init($url);          
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml',
                                            'Content-length: '.strlen($xmlSend)));
curl_setopt($ch, CURLOPT_POSTFIELDS, array("recXML" => $xmlSend));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($ch);

//Receiving XML script
if(isset($_REQUEST['recXML'])){
    $xmlTest = $_REQUEST['recXML'];
} else {
    echo "NO REQUEST XML FOUND ";
}
if(isset($HTTP_RAW_POST_DATA)){
    $xmlTest = $HTTP_RAW_POST_DATA;
} else {
    echo " NO RAW DATA ";
}
if(isset($_POST['recXML'])){
    $xmlTest = $_POST['recXML'];
}else{
    echo " NO XML RECEIVED ";
}
if(!isset($xmlTest)){
    return;
}

This is not the complete logic, but I think these are the parts that matter, if you want more code just ask.

EDIT:

after some changes and restarting my pc, it finally worked. So not exactly sure which change fixed the problem, but will post the code below in case anyone could ever find use for it.

//Sending script:     
$ch = curl_init($url);          
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml',
                                            'Content-length: '.strlen($xml)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

//receiving script
$xmlTest = file_get_contents("php://input");

echo $xmlTest;
$res = simplexml_load_string($xmlTest);
if($res === false){
    echo "Failed loading XML: ";
    foreach(libxml_get_errors() as $error){
        echo "<br>", $error->message;
    }
    return;
} else {
    //print_r($res);
}
2
  • What's the error you get ? Commented Nov 20, 2015 at 10:47
  • It didn't give a error, just the messages that I get back when the XML is empty. Commented Nov 20, 2015 at 12:00

1 Answer 1

2

You should send content-Type as application/xml so your code will become

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml',
                                            'Content-length: '.strlen($xmlSend)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlSend);

and on receiving side you can use

$xml = file_get_contents("php://input");

For your current code you can set content-type as application/x-www-form-urlencoded the you can use $_POST['recXML']

Sign up to request clarification or add additional context in comments.

4 Comments

gonna check it out, think I already tried those different combinations somewhere yesterday, but I've changed this thing up so many times, not sure anymore. atleast thanks for the response :D
Tried both your suggestions, but still get the message that no XML is found. even after removing all the other $_REQUEST, isset() etc.. but the XML variable stays empty
Just try with hardcoded xml <test></test> are you receiving any data. Might be because of size of xml
Yep.. already had that in place. really wonder where i'm making a mistake, since it seems like such a mundane task..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.