title: How to POST an XML file using cURL on php?
body: <p>I am using a local server on my computer and i am trying to make 2 php scripts to send an xml file and receive it.</p>
  
  <p>To send the xml file i use this code :</p>
  
  <pre><code>&lt;?php
    /*
     * XML Sender/Client.
     */
    // Get our XML. You can declare it here or even load a file.
    $file = 'http://localhost/iPM/books.xml';
    if(!$xml_builder = simplexml_load_file($file))
    exit('Failed to open '.$file);
  
    // We send XML via CURL using POST with a http header of text/xml.
    $ch = curl_init();
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://localhost/iPM/receiver.php");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_REFERER, 'http://localhost/iPM/receiver.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ch_result = curl_exec($ch);
    curl_close($ch);
    // Print CURL result.
    echo $ch_result;
  ?&gt;
  </code></pre>
  
  <p>To receive the xml file i use this code :</p>
  
  <pre><code>&lt;?php
    /*
     * XML Server.
     */
    // We use php://input to get the raw $_POST results.
    $xml_post = file_get_contents('php://input');
    // If we receive data, save it.
    if ($xml_post) {
      $xml_file = 'received_xml_' . date('Y_m_d-H-i-s') . '.xml';
      $fh       = fopen($xml_file, 'w') or die();
      fwrite($fh, $xml_post);
      fclose($fh);
      // Return, as we don't want to cause a loop by processing the code below.
      return;
    }
  ?&gt;
  </code></pre>
  
  <p>When i run the post script i get this error :</p>
  
  <pre><code>Notice: Array to string conversion in C:\xampp\htdocs\iPM\main.php on line 17
  </code></pre>
  
  <p>which refers to line :</p>
  
  <pre><code>curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
  </code></pre>
  
  <p>which i dont know what exactly does. The xml file i receive is created but when i open it i get this :</p>
  
  <pre><code>XML Parsing Error: syntax error
  Location: file:///C:/xampp/htdocs/iPM/received_xml_2013_01_14-01-06-09.xml
  Line Number 1, Column 1:
  </code></pre>
  
  <p>I tried to comment this specific line as i thought the problem lies there but then when i run my post script i get this error :</p>
  
  <pre><code>Request entity too large!
  
  The POST method does not allow the data transmitted, or the data volume exceeds the capacity limit.
  
  If you think this is a server error, please contact the webmaster. 
  
  Error 413
  </code></pre>
  
  <p>but the xml file is only 5kbs so this is not the problem.</p>
  
  <p>Does anyone have any idea what i should do here? All i am trying to do is make a script to send an xml file and a script to receive it and save it as an xml.</p>