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