使用CURL和Curlfile() POST multipart/form-data的完整示例


Required complete example of POST multipart/form-data using CURL and Curlfile()

我已经挣扎了几天了。(顺便说一句,我是cURL的新手)

我有API的设置由我的移动应用程序开发人员在Heroku,我张贴表单数据使用以下:

 public function do_add() {
                $Name           =   $this->input->post('Name');
                $Email          =   $this->input->post('Email');
                $Password       =   $this->input->post('Password');
                $txt             =  "Created";

                $url = 'https://myfullurl.com/api/users/';

                $fields = array(
                    'name'                      =>  $Name,
                    'email'                     =>  $Email,
                    'password'                  =>  $Password,
                    'role'                      =>  'driver'
                );
                $result = $this->scripts->api_add($url, $fields);
                        $this->session->set_userdata('Success',"Record Has Been Successfully ".$txt."...");
                        redirect(base_url().'mmsadmin/users/view_all'); 
  }

所以当我在上面发布数据时,Codeignitor函数接收到它并将其发布到下面的模型函数:

 public function api_add($url, $fields){
$fields_string = "";
                //url-ify the data for the POST
                foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
                rtrim($fields_string, '&');
                //open connection
                $ch = curl_init();
                //set the url, number of POST vars, POST data
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($ch, CURLOPT_POST, count($fields));
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->session->userdata('Token')));
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $result = json_decode($content = curl_exec($ch));
                curl_close($ch);
                return $result;
}

以上都工作良好。现在我需要能够用它发送一个文件。一直在谷歌搜索,发现了一些-F命令,并使用'@'发送它,但当我尝试我的服务器它给我错误,这个方法贬值,我应该使用curlfile()

你们哪位天才能改变我的函数来准确地显示如何做到这一点吗?因为我已经像无头苍蝇一样瞎跑了两天了(

谢谢人

curl文件发送方法从php5贬值,你应该使用curl文件类。

    // Create a cURL handle
    $ch = curl_init('http://example.com/upload.php');
    // Create a CURLFile object
    $cfile = new CURLFile('filename.fileTpye');
    // Assign POST data
    curl_file_create('filename.fileTpye');
   // Assign POST data 
    $data =     array('test_file' =>$cfile,'name'=>'test','anotherData'=>'abc');
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    // Execute the handle
    curl_exec($ch);
上面的

是面向对象的风格,另一个是下面提到的过程风格:

    $ch = curl_init('http://example.com/upload.php');
    // Create a CURLFile object
    $cfile = curl_file_create('filename.fileTpye');
    // Assign POST data
    $data = array('test_file' => $cfile);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    // Execute the handle
    curl_exec($ch);