0

I'm trying to post a file with curl in php, but the file is never uploaded/accepted by the server. I have searched and tried for several hours, but I can't find whats wrong, everyone elses examples and codes seems to work, but not this one.

Here is the code:

<?php $url = "http://jpptst.ams.se/0.52/default.aspx"; $headers = array( "Content-Type: text/xml; charset=iso-8859-1", "Accept: text/xml" ); $data = array("file" => "@documents/xmls/1298634571.xml"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, false); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $response = curl_exec($ch); curl_close($ch); var_dump($response); ?> 

The result I get:

string(904) "HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Mon, 25 Jul 2011 19:13:41 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Cache-Control: private Content-Type: text/html; charset=utf-8 Content-Length: 659" 

Thats all I get.. the file is never accepted by the server. If anyone can help me with this problem it would be much appreciated :) Thanks!

2 Answers 2

1

You're trying to upload a file via HTTP post, so sending a Content-type: text/xml header is inappropriate. An HTTP file upload is actually done as multipart/form-data, and is actually pretty much identical to a MIME-encoded email attachment. PHP's curl will fill in the header details for you automatically. As well, the Accept header is not necessary either.

Check that the path to the .xml file you're trying to upload is correct. You've not specified a leading / to it, so the path is relative to where your PHP script is executing from.

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

Comments

0

Replace:

$data = array("file" => "@documents/xmls/1298634571.xml"); 

With this:

$data = array("file" => "@".realpath('documents/xmls/1298634571.xml')); 

Try it, might work, i'm not sure tho.

EDIT:

Try this out:

<?php $xmldatafile="documents/xmls/1298634571.xml"; // Make sure the file path is correct function postData($postFields,$url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST ,1); curl_setopt($ch, CURLOPT_POSTFIELDS ,$postFileds); curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1); curl_setopt($ch, CURLOPT_HEADER ,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); $data = curl_exec($ch); curl_close($ch); return $data; } $xmlData = file_get_contents($xmldatafile); $postFileds = 'data='.$xmlData; $result = postData($postFields,"http://jpptst.ams.se/0.52/default.aspx"); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.