Есть рабочий код на Node js, пытался переписать его на PHP застрял на том, что не могу корректно отправить данные в формате multipart/form-data
async generate(prompt, model, images = 1, width = 1024, height = 1024, style = 2) {
const styles = ['KANDINSKY', 'UHD', 'ANIME', 'DEFAULT'];
const params = {
type: "GENERATE",
numImages: images,
width,
height,
style: styles[style],
generateParams: {
query: prompt
}
};
const formData = new FormData();
const modelIdData = {value: model, options: {contentType: null}};
const paramsData = {value: JSON.stringify(params), options: {contentType: 'application/json'}};
formData.append('model_id', modelIdData.value, modelIdData.options);
formData.append('params', paramsData.value, paramsData.options);
const response = await axios.post(this.URL + 'key/api/v1/text2image/run', formData, {
headers: {
...formData.getHeaders(),
...this.AUTH_HEADERS
},
'Content-Type': 'multipart/form-data'
});
const data = response.data;
return data.uuid;
}
public function generate($prompt, $model, $images = 1, $width = 1024, $height = 1024, $style = 2) {
$styles = ['KANDINSKY', 'UHD', 'ANIME', 'DEFAULT'];
$params = [
'type' => 'GENERATE',
'numImages' => $images,
'width' => $width,
'height' => $height,
'style' => $styles[$style],
'generateParams' => [
'query' => $prompt
]
];
$data = [
'model_id' => $model,
'params' => json_encode($params)
];
$ch = curl_init($this->url . 'key/api/v1/text2image/run');
$arOptions = [
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => [
"X-Key: Key 43283A1D6E71F33BBB597BD4E55E7FAA",
"X-Secret: Secret secret-key",
'Content-Type: multipart/form-data',
],
];
curl_setopt_array($ch, $arOptions);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
$data = json_decode($response, false);
if (isset($data['uuid'])) {
return $data['uuid'];
} else {
throw new Exception("Failed to generate image");
}
}