How can i post image and text to a rest API using curl in html form i am able to post both image and text using postman to the same API but it not working in curl even when i try the same image and text i used in postman Here is the code of the form.
<form method="post" action="create.php" enctype="multipart/form-data">
<label>Name</label>
<input type ="text" name="name">
<input type="file" name="user_image" accept="image/jpeg" />
<button>Submit</button>
</form>
And here is the create.php script
$ch = curl_init();
$cFile = new CURLFile(['user_image']['tmp_name'], ['user_image']['type'], ['user_image']['name']);
$data = array('name' => $name, 'user_image' => $cFile);
curl_setopt($ch, CURLOPT_URL, "rest_api_url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
curl_close($ch);
Am able to post text without any issues but when i try to post with image nothing is been sent to the api endpoint. PS: Am new to PHP so my process could be wrong am not sure thanks Php script that handles the image
if(is_uploaded_file($_FILES["user_image"]["tmp_name"]) && $_POST["name"]){
$tmp_file = $_FILES["user_image"]["tmp_name"];
$img_name = $_FILES["user_image"]["name"];
$upload_dir ="./Images/".$img_name;
$sql = "INSERT INTO tbl_cc(name, im) VALUES('{$_POST['name']}', '{$img_name}')";
//
if(move_uploaded_file($tmp_file, $upload_dir) && $conn->query($sql)){
$respone["Message"] = "Success";
} else {
$respone["Message"] = "Failed";
}
} else {
$respone["Message"] = "Invalid";
}