Does CURL add spaces to publish content?

I am trying to do a POST on a provider server using PHP 5.2 with cURL. I read in an XML document to publish on my server, and then read in response:

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

This code itself works fine, but another server returns an XML parser response from it, which says:

Content not allowed in prolog

I looked at this error, and it is usually caused by spaces before XML, but I made sure that the XML file itself does not have spaces, and trim () should still clear it. I did TCPDump in the connection while I was running the code, and this is what was sent:

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

There are visible spaces before and after the [SNIP] line when I play a session in Ethereal. Is this what causes the problem, and if so, how can I remove it or am I looking too far, and this may be a problem with the server to which I am sending a message?

+4
source share
4 answers

It turns out this is an encoding problem. It looks like the application needs XML in www-form-urlencoded instead of form data, so I had to change:

 # This sets the encoding to multipart/form-data curl_setopt($curlHandle, CURLOPT_POSTFIELDS, array('XML'=>$request)); 

to

 # This sets it to application/x-www-form-urlencoded curl_setopt($curlHandle, CURLOPT_POSTFIELDS, 'XML=' . urlencode($request)); 
+2
source

Not an answer, but I find that all fopen / fread / fclose is very boring to read when looking at the code.

You can replace:

 $file = 'test.xml'; $fileHandle = fopen($file, 'r'); $request = fread($fileHandle, filesize($file)); fclose($fileHandle); $request = trim($request); 

WITH

 $request = trim(file_get_contents('test.xml')); 

But in any case - to your question; if these are the headers that are sent, then this should not be a problem with the remote server. Try changing the contents of your xml file and using var_dump () check the exact result (including the length of the string so you can look for missing things)

Hope that helps

+3
source

I did wc -m test.xml and returned with 743 characters in the XML file, and var_dump on $request returns with 742 characters, so something is pulled together with trim() (I assume).

I did:

 print "=====" . $request . "====="; 

and the beginning and end of the XML butt is directly against ===== without spaces.

0
source

Check out this answer to another question - I had a similar question with you, and it turned out that it helped me.

0
source

Source: https://habr.com/ru/post/1276382/


All Articles