1

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";
   }
Serenity
  • 99
  • 1
  • 11
  • 1
    It's a little hard for us to help you when we don't know where you are posting the image to. How is the destination where you post your image to handling the upload of that image? – Niels Mar 18 '22 at 02:14
  • @Niels I have update the question with the php script that handles the image and text upload – Serenity Mar 18 '22 at 02:19
  • 1
    Have you checked if PHP has enough write permission to put images into that folder? – Niels Mar 18 '22 at 02:30
  • yes like i said am able to post correctly using postman both text and image which i don't think will be possible if there are permission issues. – Serenity Mar 18 '22 at 02:33
  • 3
    @Serenity Is there any need to do curl to the other php code? I felt it's redundant. 2nd is are you already set the curl into post mode? https://stackoverflow.com/a/31995260/4906348 – Benyamin Limanto Mar 18 '22 at 02:40
  • @BenyaminLimanto am not sure what other php code u are referring to am only using the curl in the create.php script – Serenity Mar 18 '22 at 02:43
  • @Serenity if everything posts just fine, why do you need CURL is what he is asking? – Niels Mar 18 '22 at 02:45
  • @Niels thanks for the clarification really sorry pardon my confusion didn't realize i could send to the API script directly from the html form – Serenity Mar 18 '22 at 02:56
  • @Serenity use `curl_setopt($ch, CURLOPT_POST, 1 );` rather than `curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");`, I never seen any of that on php.net docs regarding customrequest... – Benyamin Limanto Mar 18 '22 at 02:56
  • @BenyaminLimanto thanks followed a tutorial guess it outdated – Serenity Mar 18 '22 at 03:11

0 Answers0