Send multiple images via Curl

Asked

Viewed 570 times

1

Hello, I need to send via Curl multiple images, which will be received in an API, but in the API it receives in a specific way, in which I am not able to send by Curl. Follows form the API receives the image:

foreach ($_FILES as $file) {
                $arquivo_enviar = $file['name'];
                $extensao_arquivo = strrchr($arquivo_enviar, '.');
                $arquivo_enviar = md5(time().$arquivo_enviar) . $extensao_arquivo;
                if (in_array($file['type'], $allowedExts)) {
                    if ($file["error"] > 0) {
                        http_response_code(406);
                    } else{
                        $de = $_SERVER['DOCUMENT_ROOT'].'everton/api/uploads/produtos_'.md5($data->authenticator).$id_loja.'/'. $arquivo_enviar;
                        move_uploaded_file($_FILES["file"]["tmp_name"], $de);
                        $caminho[$i] = 'http://192.168.110.4/everton/api/uploads/produtos_'.md5($data->authenticator).$id_loja.'/'.$arquivo_enviar;

in this case, the $_FILES contains the following information:

Array(  
   [file] => Array
   (
       [name] => blog_pense_digital_gif.gif
       [type] => image/gif
       [tmp_name] => /tmp/phpkoZilo
       [error] => 0
       [size] => 761850
   ))

At the moment, I’m sending, by Curl, as follows:

$url = $url_api.'produto/'.$produtoId.'/uploadimage?authenticator='.$token;
            $imagens = count($_FILES["file"]["name"]);
            for ($i = 0; $i < $imagens; $i++){
                $filename = $_FILES['file']['name'][$i];
                $filedata = $_FILES['file']['tmp_name'][$i];
                $filesize = $_FILES['file']['size'][$i];
                if ($filedata != ''){
                    $headers = array("Content-Type:multipart/form-data");
                    $postfields = array("filedata" => "@$filedata", "filename" => $filename);
                    $ch = curl_init();
                    $options = array(
                        CURLOPT_URL => $url,
                        CURLOPT_HEADER => true,
                        CURLOPT_POST => 1,
                        CURLOPT_HTTPHEADER => $headers,
                        CURLOPT_POSTFIELDS => $postfields,
                        CURLOPT_INFILESIZE => $filesize,
                        CURLOPT_RETURNTRANSFER => true
                    );
                    curl_setopt_array($ch, $options);
                    print curl_exec($ch);
                    curl_close($ch);
                }
}

Only the API is getting my file as follows:

Array(
[filename] => Sem título.jpg)
Array(
[filedata] => Array
    (
        [name] => phpKERYcS
        [type] => application/octet-stream
        [tmp_name] => /tmp/phpggdoxS
        [error] => 0
        [size] => 168099
    ))

The filename is being sent separately from the rest, via POST, and filedata, with the file information via FILE

  • 1

    Please enter your code in a codePen so that we can view it better and so we can help you?

1 answer

0

Follows solution:

 $produtoId = $_POST['produtoId'];       
            $url = $url_api.'produto/'.$produtoId.'/uploadimage?authenticator='.$token; 
            $total_imagens = count($_FILES['file']['name']);
            for ($i=0; $i < $total_imagens; $i++) { 
                $target_path = $_FILES['file']['tmp_name'][$i];
                $filename = $_FILES['file']['name'][$i];
                $filetype = $_FILES['file']['type'][$i];
                $filedata = $_FILES['file']['tmp_name'][$i];
                $postfields = array("filedata" => "@$filedata;type=".$filetype, "filename" => $filename);
               // $postfields = array('files' => $target_path);
                $filesize = $_FILES['file']['size'];
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields );
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data'));
                curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                print curl_exec($ch);
                curl_close($ch);  
            } 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.