M
M
maryst19862021-05-23 10:22:53
API
maryst1986, 2021-05-23 10:22:53

Please check and explain what is wrong with api multipart/form-data?

It is necessary to send a photo from the server via api to another server.
The request must be formed as follows
https://suppliers-api.wildberries.ru/card/upload/f...

The header must contain the following fields:

- X-File-ID - generated file identifier in uuid v4 format must be transferred to the product card. The provider generates the ID on its own.
- Authorization – token received in the personal account (example WBToken=AvvYxgfMn7P0C8zCuPQLQhkkkt-C…)
- X-Supplier-ID – Supplier-ID in the uuid format that was issued during registration

A file is sent to the request body. Request example:

```json
POST https://suppliers-api.wildberries.ru/card/upload/f...
Authorization: {{token}}
Content-Type: multipart/form-data; boundary=boundaryImage
X-File-Id: 00000000-0000-0000-0000-000000000001
X-Supplier-ID: 00000000-0000-0000-0000-000000000001

--boundaryImage
Content-Disposition: form-data; name="uploadfile"; filename="photo.jpg"

bytes photo.jpg
--boundaryImage--
```

An example CURL request that can be used to download a file is

```
$ curl -X POST --location " https://suppliers-api.wildberries. ru/upload/file/m... " \
-H "Authorization: {{token}}" \
-H "X-Supplier-ID: fb25c9e9-cae8-52db-b68e-736c1466a3f5" \
-H "X-File -id: 00000000-0000-0000-0000-000000000001" \
-H "Content-Type: multipart/form-data; boundary=img" \
-F "[email protected]/photo.jpg;filename=photo.jpg;type=*/*"
```

We implemented it like this

function post_image($clientId, $apiKey, $method, $data,$uid_pict,$supplierID){
  $url = 'https://suppliers-api.wildberries.ru'.$method;
  $headers = array(     
  'Authorization: '.$apiKey,
   'X-File-Id: '.$uid_pict.'',
   'X-Supplier-ID: '.$supplierID.'',
    'Content-Type: multipart/form-data; boundary=boundaryImage'
  ) ;                        

  $ch = curl_init();
  $options = array(
    CURLOPT_URL => $url,         
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => false,
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_HTTPHEADER => $headers
  );		

  curl_setopt_array($ch, $options); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
print_r($headers);
  
  $html = curl_exec($ch);
  curl_close($ch); 
  return $html;
};

Файл в котором используется эта функция

<?php 
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<head>
erfedrf    
    <title> </title>
</head>';
 		require  "connect.html";
    $database="client_5df968f_f_zoocopy";
  
    mysql_select_db($database); $database="client_5df968f_f_zoocopy";mysql_select_db($database);
mysql_query("SET NAMES 'cp1251' COLLATE 'cp1251_general_ci'");
    mysql_query("SET CHARACTER SET 'cp1251'");
    echo $sql="TRUNCATE TABLE wi_fbs1";
    
    //$q = mysql_query($sql,$conn) or die();
  
    ?>


<?php 


  include 'wi_api.php';
  
   	
    $sql="SELECT * FROM wi_fbs1 where donephoto=0  and uid_pict>'' order by `id` limit 0,1 ";

    $q2 = mysql_query($sql,$conn) or die();
    $n2=mysql_num_rows($q2);	
    



 		//$n2=1;
 	 $j=0;
  // $n2=1;
for($jq=0;$jq<$n2;$jq++)
{
  echo "<br><br>";
        
        $uid_pict=mysql_result($q2,$j,'uid_pict');
        $articul=mysql_result($q2,$j,'articul');
        $nmid=mysql_result($q2,$j,'nmid');
         echo "<br><br>"; 
         $dir=__DIR__;
      echo 	$picture=str_replace("https://files.zoostar.spb.ru","",mysql_result($q2,$j,'picture'));
     echo "<br><br>";
     $pict=explode('/',$picture);
     $pict1=$pict[sizeof($pict)-1];
 echo $fileLocation = str_replace("zoostar.spb.ru/base","files.zoostar.spb.ru",$dir.$picture);
  echo "<br><br>";
echo 'is real file:';
var_dump(is_file($fileLocation));
 
 $filesize = getimagesize($fileLocation);

   echo    $request ='\r\n\r\n--boundaryImage\r\n
Content-Disposition: form-data; name="uploadfile"; filename="'.$fileLocation.'"\r\n
Content-Type: image/jpeg\r\n
Content-Transfer-Encoding: binary\r\n
 '.file_get_contents($fileLocation).'\r\n
--boundaryImage--\r\n';
    
           $method = "/card/upload/file/multipart"; //метод запроса
        //print_r($request);echo "<br><br>";
        echo "<br><br>";
        //echo $app_secret;
        echo "<br><br>";
     		$result = post_image($client_id, $app_secret, $method, $request,$uid_pict,$supplierID); //вывод результата
    
}

    
 
    
  
    
?>

response : Error when parsing request
What's wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question