duanbozhong9689 2013-11-28 05:56
浏览 137

PHP使用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

  • 写回答

1条回答 默认 最新

  • douhuan5073 2013-11-28 07:20
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程