Attachments
Upload attachments
When adding/updating record via the API, you have the option to send attachments for fields such as Attachments, File, Images. There are two ways to upload a file:
1) Send the file by http link
$item = array(
'field_158' => 'Test api PROJECT ',
'field_242' => 'https://www.rukovoditel.net.ru/images/docs/dogovor_template.docx',
'field_162' => 'https://www.rukovoditel.net.ru/images/docs/dogovor_template.jpg',
);
For the attachment field, you can pass multiple links separated by commas.
2) Send the contents of the file.
$files = [];
$files[] = ['name'=>'file1.pdf','content'=>base64_encode(file_get_contents('file1.pdf'))];
$files[] = ['name'=>'file2.jpg','content'=>base64_encode(file_get_contents('file2.jpg'))];
$item = array(
'field_158' => 'Test api project ',
'field_162' =>$files,
);
In this example, the field_162
field is a field with the "Attachments" type. An array of two files is passed to this field. The file name and its content are specified in the array. The content must be encoded in base64
.
Updating attachments
When updating a record, you may need to add an attachment to existing files. To update attachments, you first need to get the name of the files of an existing record and then send only the names of the files, without the "content" parameter.
//get record info with ID 33
$params = array(
'key' => '58p3crBBgK8Y5wzN5QIjI0cHeG78uibvt9xyuZaa',
'username' => 'admin',
'password' => 'admin',
'action' => 'select',
'entity_id' => 21,
'select_fields' => '162',
'filters' => ['id'=>33],
);
//...
$result = json_decode($result,true);
//Add exist files names without "content" data
$files = [];
foreach(explode(',',$result['data'][0][162]) as $filename)
{
$files[] = ['name'=>$filename];
}
//Add new files to upload
$files[] = ['name'=>'file3.jpg','content'=>base64_encode(file_get_contents('file3.jpg'))];
$data = array(
'field_158' => 'Test api project' ,
'field_162' =>$files,
);
Download attachments
To download a file via the API, there is a download_attachment
method. When calling the method, you need to pass the record ID and the ID of the field in which the file is stored.
$params = array(
'key' => 'KUhUvp8sGZv1vIlQATEQXr4HJT4SsWEjd8LWGSN6',
'username' => 'admin',
'password' => 'admin',
'action' => 'download_attachment',
'entity_id' => 21,
'item_id' => 1,
'field_id' => 207,
);
//...
if($result)
{
$result = json_decode($result,true);
if($result['status']=='success')
{
file_put_contents($result['data']['filename'], base64_decode($result['data']['content']));
}
}
If successful, the program returns the file name $result['data']['filename']
and its contents $result['data']['content']
in base64 encoding.
Please note: if multiple files are stored in a field, a zip archive with all files will be returned.
Delete attachments
To delete a file via the API, there is a delete_attachment
method. When calling the method, you need to pass the record ID and the ID of the field in which the file is stored.
$params = array(
'key' => 'KUhUvp8sGZv1vIlQATEQXr4HJT4SsWEjd8LWGSN6',
'username' => 'admin',
'password' => 'admin',
'action' => 'delete_attachment',
'entity_id' => 21,
'item_id' => 2,
'field_id' => 162,
);
//...
if($result)
{
$result = json_decode($result,true);
}
If successful, the program will return the names of the deleted files.