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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿