1

I need some help please.

I have a php script which generates a xml file and then i need to submit this to another server using POST. The XML file is ok as i can manually upload it using curl from the command line.

my code is:

$file = 'temp.xml';
file_put_contents($file, $output);
$post = array('extra_info' => '123456','file_contents'=>'@'.$file);
$url = "<URL>";
$chlead = curl_init();
curl_setopt($chlead, CURLOPT_USERPWD, "<USER>:<PASSWORD>");
curl_setopt($chlead, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($chlead, CURLOPT_URL, $url);
curl_setopt($chlead, CURLOPT_VERBOSE, 1);
curl_setopt($chlead, CURLOPT_POST,1);
curl_setopt($chlead, CURLOPT_POSTFIELDS, $post);
curl_setopt($chlead, CURLOPT_RETURNTRANSFER, false);
$response = curl_exec($chlead);

I can manually upload the file to the server which handles is correctly by:

curl -v -k -u <USER>:<PASSWORD> -X POST --upload-file temp.xml <URL>

What am i missing? I am using PHP 5.2.17. Also if there is a way of doing this without writing the xml file to disk that would be good to know.

Thanks

  • You need to submit it like a form with the headers. Take a look at the answer here: http://stackoverflow.com/questions/3085990/post-a-file-string-using-curl-in-php – Nahydrin Nov 28 '13 at 07:24
  • Thanks Brian! I had to add the code: `curl_setopt($chlead, CURLOPT_HTTPHEADER , array( 'Content-Type: text/xml', 'Content-Length: ' . strlen($output))); ` – user3044492 Nov 28 '13 at 21:17

1 Answers1

0

check the target and the file url path is correct or use the absolute path

user3040610
  • 750
  • 4
  • 15