Php 使用CURL和Curlfile()发布多部分/表单数据的必需完整示例

Php 使用CURL和Curlfile()发布多部分/表单数据的必需完整示例,php,post,curl,heroku,multipartform-data,Php,Post,Curl,Heroku,Multipartform Data,我已经挣扎了几天了。(顺便说一句,我是卷发新手) 我的移动应用程序开发人员在Heroku上设置了API,我使用以下方式发布表单数据: public function do_add() { $Name = $this->input->post('Name'); $Email = $this->input->post('Email');

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

我的移动应用程序开发人员在Heroku上设置了API,我使用以下方式发布表单数据:

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

  }
所以,当我在Codeignator函数上方发布数据时,收到了数据并将其发布到下面的模型函数中:

 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中折旧,您应该使用curlfile类

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

有关更多信息,请参阅php手册。

谢谢,但这只是发送文件的一个示例。我如何发送表单数据和文件。我的开发人员说,他设置接收的方式是获取表单数据,然后接收文件。但显然我必须同时发送。您可以在数据数组中附加数据,如:
$cfile=curl\u file\u create('filename.fileTpye');//分配POST数据$data=array('test\u file'=>$cfile,'name'=>'test','anotherData'=>'abc');