<p>I am attempting to POST against a vendor's server using PHP 5.2 with cURL. I'm reading in an XML document to post against their server and then reading in a response:</p>

<pre><code>$request = trim(file_get_contents('test.xml'));
$curlHandle = curl_init($servletURL);
curl_setopt($curlHandle, CURLOPT_POST, TRUE);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, array('XML'=&gt;$request));
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curlHandle, CURLOPT_HEADER, FALSE);  # Have also tried leaving this out
$response = curl_exec($curlHandle);
</code></pre>

<p>That code, in an of itself, works OK, but the other server returns a response from it's XML parser stating:</p>

<blockquote>
  <p>Content not allowed in prolog</p>
</blockquote>

<p>I looked that error up and this is normally caused by whitespace before the XML, but I made sure that the XML file itself has no whitespace and the trim() should clear that up anyway. I did a TCPDump on the connection while I ran the code and this is what is sent out:</p>

<pre><code>POST {serverURL} HTTP/1.1
Host: {ip of server}:8080
Accept: */*
Content-Length: 921
Expect: 100-continue
Content-Type: multipart/form-data; boundry:---------------------------01e7cda3896f

---------------------------01e7cda3896f
Content-Disposition: form-data; name="XML"

[SNIP - the XML was displayed]

---------------------------01e7cda3896f--
</code></pre>

<p>Before and after the [SNIP] line there is visible whitespace when I replay the session in Ethereal. Is this what is causing the problem and, if so, how can I remove it, or am I looking too far and this may be an issue with the server I'm posting against?</p>

[{'answer_id': 18215, 'author': 'mercutio', 'author_id': 1951, 'author_profile': 'https://Stackoverflow.com/users/1951', 'pm_score': 2, 'selected': False, 'text': "<p>Not an answer, but I find the whole fopen/fread/fclose thing very dull to peruse when looking at code.</p>\n\n<p>You can replace:</p>\n\n<pre><code>$file = 'test.xml';\n$fileHandle = fopen($file, 'r');\n$request = fread($fileHandle, filesize($file));\nfclose($fileHandle);\n$request = trim($request);\n</code></pre>\n\n<p>With:</p>\n\n<pre><code>$request = trim(file_get_contents('test.xml'));\n</code></pre>\n\n<p>But anyway - to your question; if those are the headers that are being sent, then it shouldn't be a problem with the remote server. Try changing the contents of your xml file and using var_dump() to check the exact output (including the string length, so you can look for missing things)</p>\n\n<p>Hope that helps</p>\n"}, {'answer_id': 18247, 'author': 'dragonmantank', 'author_id': 204, 'author_profile': 'https://Stackoverflow.com/users/204', 'pm_score': 0, 'selected': False, 'text': '<p>I did a <code>wc -m test.xml</code> and came back with 743 characters in the XML file and the <code>var_dump</code> on <code>$request</code> comes back with 742 characters so something is getting stripped with <code>trim()</code> (I assume).</p>\n\n<p>I did a:</p>\n\n<pre><code>print "=====" . $request . "=====";\n</code></pre>\n\n<p>and the start and end of the XML butts right up against the ===== with no white space.</p>\n'}, {'answer_id': 18287, 'author': 'dragonmantank', 'author_id': 204, 'author_profile': 'https://Stackoverflow.com/users/204', 'pm_score': 3, 'selected': True, 'text': "<p>It turns out it's an encoding issue. The app apparently needs the XML in www-form-urlencoded instead of form-data so I had to change:</p>\n\n<pre><code># This sets the encoding to multipart/form-data\ncurl_setopt($curlHandle, CURLOPT_POSTFIELDS, array('XML'=&gt;$request));\n</code></pre>\n\n<p>to</p>\n\n<pre><code># This sets it to application/x-www-form-urlencoded\ncurl_setopt($curlHandle, CURLOPT_POSTFIELDS, 'XML=' . urlencode($request));\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/18166', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/204/']