title: PHP send and receive XML
body: <p>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. </p>
  
  <p>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.</p>
  
  <pre><code>//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" =&gt; $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;
  }
  </code></pre>
  
  <p>This is not the complete logic, but I think these are the parts that matter, if you want more code just ask.</p>
  
  <p>EDIT:</p>
  
  <p>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.</p>
  
  <pre><code>//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 "&lt;br&gt;", $error-&gt;message;
      }
      return;
  } else {
      //print_r($res);
  }
  </code></pre>