dongyanju0945 2015-07-10 04:51
浏览 52

使用CURL和Curlfile()获得POST多部分/表单数据的完整示例

I have struggling couple of days now. (Btw, I'm new to cURL)

I have API's setup by my mobile application developer on Heroku where I'm posting form data using following:

 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'); 

  }

So when I post data above function of Codeignitor received it and post it to my model function below:

 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;

}

Above all working fine. Now I need to be able to send a file with it. Have been googling and found some -F command and also sending it using '@' but when I try that my server it give me error that this method is depreciated and I should use curlfile()

Can one of you geniuses out there change my functions to show exactly how to do this. As I'm clueless and running around like headless chicken for two days now :(

Thanks guys

  • 写回答

1条回答 默认 最新

  • dqoag62688 2015-07-10 05:28
    关注

    curl file sending method you are using is depreciated from php5 and you should use curlfile class.

        // 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);
    

    above is Object oriented style,another is Procedural style which is mentioned below:

        $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);
    

    for more information please see php curlFile manual.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分