简体   繁体   中英

PHP upload file from server to another server using curl post

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

检查目标和文件URL路径是否正确或使用绝对路径

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM