0

I'm trying to send xml to a rest server.

$url = url;
$file = 'finn/test.xml';
$post = array('name' => 'fil','file_contents'=>'@'.$file); 

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

When I run this code I get a blank response.

I know there's nothing wrong with the server or the xml because when I use this to just upload the file to the server everything works fine.

<html>
<body>
<form name="bilupload" action="url" method="post" enctype="multipart/form-data">
partner: <input TYPE="FILE" NAME="fil" size="10">
<br>
<input type="submit" value="Send">
</form>
</body>
</html>

curl_getinfo prints this:

[url] => url
[content_type] => text/html;charset=ISO-8859-1
[http_code] => 200
[header_size] => 497
[request_size] => 144
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.062231
[namelookup_time] => 2.7E-5
[connect_time] => 0.016873
[pretransfer_time] => 0.016928
[size_upload] => 3668
[size_download] => 35
[speed_download] => 562
[speed_upload] => 58941
[download_content_length] => 35
[upload_content_length] => 3668
[starttransfer_time] => 0.034394
[redirect_time] => 0

I've been struggling with this for quite some time and I would be very grateful if anyone could point me in the right direction.

4
  • You're not uploading a file, you're simply sending the xml contents as the request body. Commented Aug 27, 2013 at 15:32
  • "$xmlcontent" is a symptom of cargo-cult programming. why not just $xmlcontent? Commented Aug 27, 2013 at 15:59
  • Maerlyn: Yes, you're right. I fixed it. But I still have the same problem. Commented Aug 28, 2013 at 9:46
  • Marc B: Yes, you're also right. I was copying and pasting a lot of code. $xmlcontent is better. Commented Aug 28, 2013 at 9:47

1 Answer 1

2

Your form tells the server that it's sending form data in a given format:

enctype="multipart/form-data"

But your PHP scripts tells it's sending XML (but doesn't send XML at all):

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));

I suppose you want this:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));

... though you can just omit the line.

Perhaps you had the Accept header in mind :-?

Sign up to request clarification or add additional context in comments.

1 Comment

Yep. At first I was trying to send the xml content in the request body. Then I changed it to a file. So yes, multipart/form-data worked a lot better. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.