Файлы сначала загружаются в папку, и после этого по id прикрепляем к сделке.
Есть вот это, но оно затирает все что было в этом поле.
<?php
$webhook_url = 'https://site.ru/rest/1/rfmz03lddfg4rx4p48/';
$deal_id = 43572; // ID сделки
$file_paths = [
'data_empty.csv',
'data.csv',
]; // Массив файлов для загрузки
$custom_field = 'UF_CRM_1622097411131'; // Код пользовательского поля
// 1. Получаем текущие файлы из сделки
function getDealFiles($webhook_url, $deal_id) {
global $custom_field;
$response = makeRestRequest($webhook_url.'crm.deal.get', ['id' => $deal_id]);
if (isset($response['result'])) {
if ($response['result'][$custom_field]) {
$value = $response['result'][$custom_field];
return [
'field_name' => $custom_field,
'files' => is_array($value) ? $value : [$value]
];
}
}
return ['field_name' => '', 'files' => []];
}
// 2. Загружаем файлы на сервер
function uploadFiles($webhook_url, $file_paths) {
global $deal_id;
$uploaded_files = [];
foreach ($file_paths as $path) {
if (!file_exists($path)) continue;
$file_name = basename($path);
$file_base64 = base64_encode( file_get_contents($path) );
$params = [
'id' => '55722', // - корневая папка пользователя
'data' => [
'NAME' => $file_name
],
'fileContent' => [
$file_name,
$file_base64
],
'generateUniqueName' => true
];
/*
echo '<pre>';
print_r($params);
echo '</pre>';
*/
$response = makeRestRequest($webhook_url.'disk.folder.uploadfile.json', $params);
//print_r($response);
//die();
if (isset($response['result'])) {
$uploaded_files[] = $response['result']['ID'];
}
}
return $uploaded_files;
}
// 3. Обновляем сделку с новыми файлами
$current_files = getDealFiles($webhook_url, $deal_id);
$new_files = uploadFiles($webhook_url, $file_paths);
if (!empty($new_files)) {
// Объединяем старые и новые файлы
$all_files = array_merge($current_files['files'], $new_files);
/*
echo '<pre>';
print_r($all_files);
die();
*/
// Обновляем сделку
$params = [
'id' => $deal_id,
'fields' => [$custom_field => $all_files]
];
$response = makeRestRequest($webhook_url.'crm.deal.update', $params);
if (isset($response['result'])) {
echo "Файлы успешно прикреплены к сделке!";
} else {
echo "Ошибка: ".print_r($response, true);
}
}
// Вспомогательная функция для REST-запросов
function makeRestRequest($url, $params = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
?>
print_r($all_files); выдает вот такое.
Array
(
[0] => Array
(
[id] => 153965
[showUrl] => /bitrix/components/bitrix/crm.deal.show/show_file.php?ownerId=43572&fieldName=UF_CRM_1622097411131&dynamic=Y&fileId=153965
[downloadUrl] => /bitrix/components/bitrix/crm.deal.show/show_file.php?auth=&ownerId=43572&fieldName=UF_CRM_1622097411131&dynamic=Y&fileId=153965
)
[1] => 55780
[2] => 55781
)