drnzpd579935 2011-01-20 18:21
浏览 256
已采纳

使用CURL和PHP将文件发送到HTTPS URL

I am attempting to send a file to an Https URL with this code:

$file_to_upload = array('file_contents'=>'@'.$target_path); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $target_url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSER, FALSE);
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file='.$file_to_upload); 
$result = curl_exec($ch); 
$error = curl_error($ch);
curl_close ($ch); 
echo " Server response: ".$result; 
echo " Curl Error: ".$error;

But for some reason I'm getting this response:

Curl Error: Failed to open/read local data from file/application

Any advice would help thanks!

UPDATE: When I take out CURLOPT_UPLOAD, I get a response from the target server but it says that there was no file in the payload

  • 写回答

2条回答 默认 最新

  • duanang58939 2011-01-20 18:32
    关注

    You're passing a rather strange argument to CURLOPT_POSTFIELDS. Try something more like:

    <?
    $postfields = array('file' => '@' . $target_path);
    // ...
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    ?>
    

    Also, you probably want CURLOPT_RETURNTRANSFER to be true, otherwise $result won't get the output, it'll instead be sent directly to the buffer/browser.

    This example from php.net might be of use as well:

    <?php
    
    $ch = curl_init();
    
    $data = array('name' => 'Foo', 'file' => '@/home/user/test.png');
    
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    curl_exec($ch);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了