0

I have a form I want to post all field to the rest api . I want to submit file and content both. How can I retrieve that on my API ? I have written the api using raw PHP . In my api i have check the header optcode so i need to pass the http header property also. I have tried using CUrl this way i can pass only data not file . is there any easy way to pass file and content . This is my form data

<div class="layout-content">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <h2>Local Vegitable Upload</h2>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <form action="" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Plant Name:</label>
                                    <input type="text" name="plant_name" class="form-control" id="email"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Scientific Name:</label>
                                    <input type="text" name="scientific_name" class="form-control" id="email"/>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">English Name:</label>
                                    <input type="text" name="eng_name" class="form-control" id="eng_name"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Local Name:</label>
                                    <input type="text" name="local_name" class="form-control" id="local_name"/>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Family:</label>
                                    <input type="text" name="family" class="form-control" id="family"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Botany:</label>
                                    <input type="text" name="botany" class="form-control" id="botany"/>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Usage:</label>
                                    <input type="text" name="useg" class="form-control" id="useg"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Caution :</label>
                                    <input type="text" name="caution" class="form-control" id="caution"/>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Place:</label>
                                    <input type="text" name="place" class="form-control" id="place"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Referent :</label>
                                    <input type="text" name="referent" class="form-control" id="referent"/>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Image 1 :</label>
                                    <input type="file" name="image[]" class="form-control" id="referent"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plantName">Image 2 :</label>
                                    <input type="file" name="image[]" class="form-control" id="referent"/>
                                </div>
                            </div>
                        </div>


                        <div class="form-group">
                            <label for="discription">Description:</label>
                            <textarea type="description" class="form-control" id="description"></textarea>
                        </div>
<!--                        <div class="checkbox">-->
<!--                            <label><input type="checkbox"> Remember me</label>-->
<!--                        </div>-->
                        <button type="submit" class="btn btn-default">Submit</button>
                    </form>
                </div>
            </div>

        </div>
    </div>

previously i have tried in this way

<?php

if (isset($_POST['plant_name']) && isset($_POST['description'])) {

    $plant_name = $_POST['plant_name'];
    $scientific_name = $_POST['scientific_name'];
    $eng_name =  $_POST['eng_name'];
    $local_name = $_POST['local_name'];
    $family = $_POST['family'];
    $botany = $_POST['botany'];
    $useg = $_POST['useg'];
    $caution = $_POST['caution'];
    $place = $_POST['place'];
    $referent = $_POST['referent'];
    $description = $_POST['description'];

    $data = "{\"plant_name\":\"$plant_name\",\"scientific_name\":\"$scientific_name\" ,\"eng_name\":\"$eng_name\",\"local_name\":\"$local_name\",\"family\":\"$family\",\"botany\":\"$botany\" ,\"useg\":\"$useg\",\"caution\":\"$caution\",\"place\":\"$place\" ,\"referent\":\"$referent\" ,\"description\":\"$description\"}";


    $apiResult = callLocVegApi("locveg" , $data);
     function callLocVegApi($optcode,$data){
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_PORT => APIPORT,
    CURLOPT_URL => APIURL,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        "content-type: multipart/form-data",
        "optcode: $optcode"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    return "cURL Error #:" . $err;
} else {
    return $response;
}

}

    $result =json_decode($apiResult, true);

    if($result["result"]==1){


        header('Location: dashboard.php');


    }else{

//        echo '<script>alert("Please Check Username or Password");</script>';
    }

}
?>
enter code here
Abaneel Jahid
  • 135
  • 10
  • Possible duplicate of [Call a REST API in PHP](https://stackoverflow.com/questions/9802788/call-a-rest-api-in-php) – AA Shakil Jan 22 '18 at 18:14
  • No i want to upload file also that question not cover file upload – Abaneel Jahid Jan 22 '18 at 19:32
  • Duplicate of [Posting raw image data as multipart/form-data in curl](https://stackoverflow.com/questions/21905942/posting-raw-image-data-as-multipart-form-data-in-curl) – kamal Mar 13 '20 at 07:36

0 Answers0