dpik71879 2016-02-05 21:28
浏览 93

将输入文件从表单传递到cURL POST

I have a form with a file upload field which POSTs to an endpoint of mine. From there, I'd like to POST the file to an API I use.

What's the best way of achieving this?

Do I have to move the file from temporary storage in order to POST it to the API? Or maybe I have to add cURL options?

HTML:

<form method="post" action="/my_endpoint">
    <div class="form-group">
        <label for="resume">Resume*</label>
        <input type="file" class="form-control" id="resume" name="resume" placeholder="Resume" required />
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

PHP:

$f3->route('POST /my_endpoint',
    function($f3) {
        $url = API_ENDPOINT;
        $post_params = $f3->get('POST');
        $files_params = $f3->get('FILES');

        $fields = array(
            'id' => $post_params['id'],
            'email' => $post_params['email'],
            'resume' => '@'.$files_params['resume']['tmp_name']
        );

        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
        rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
        $result = curl_exec($ch);
        curl_close($ch);
    }
);
  • 写回答

1条回答 默认 最新

  • dou72260 2016-02-06 00:47
    关注

    I took @Barmar's advice and created a curlfile.

    $f3->route('POST /my_endpoint',
        function($f3) {
            $url = API_ENDPOINT;
            $post_params = $f3->get('POST');
            $files_params = $f3->get('FILES');
    
            $resume_file = curl_file_create(realpath($files_params['resume']['tmp_name']),$files_params['resume']['type'],$files_params['resume']['name']);
    
            $fields = array(
                'id' => $post_params['id'],
                'email' => $post_params['email'],
                'resume' => $resume_file
            );
    
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL, $url);
            curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
            curl_setopt($ch,CURLOPT_POST, count($fields));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
            $result = curl_exec($ch);
            curl_close($ch);
        }
    );
    

    And then it worked.

    评论

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!