简体   繁体   中英

PHP send and receive XML

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

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM