reutskiy-a / simple-api-bitrix24
Simple REST API Bitrix24 client: OAuth 2.0, Webhook, flexible DB support, app installer, API Limit Handling Service
Requires
- php: ^8.1
- aura/sqlquery: ^2.0 || ^3.0
- guzzlehttp/guzzle: ^7.0
- monolog/monolog: ^2.0 || ^3.0
Requires (Dev)
- guzzlehttp/psr7: ^2.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.0 || ^10.0 || ^11.0 || ^12.0
This package is not auto-updated.
Last update: 2025-05-22 12:11:37 UTC
README
Simple REST API Bitrix24 client: OAuth 2.0, Webhook, flexible DB support, app installer, REST API Limit Handling Service
Клиент для REST API Bitrix24:
OAuth 2.0 (с автообновлением токенов), Webhook, поддержка всех популярных реляционных БД, менеджер установки локальных/тиражных приложений. Установка одного локального приложения на несколько порталов.
Встроенная обработка лимитов REST API
Installation
composer require reutskiy-a/simple-api-bitrix24
LANGUAGE:
Local/distributed app installation example:
Русский:
Содержание:
-
Соединение OAuth 2.0 (Локальное или Тиражное приложение)
2.1. Подготовка базы данных для хранения токенов
2.2. Создание объекта соединения OAuth 2.0
2.3. Установка приложения
-
4.1. Debug логирование
-
5.1 Стандартный пакетный запрос
5.2 Батч сервис
1. Быстрый старт Webhook соединение
use SimpleApiBitrix24\ApiClientSettings; use SimpleApiBitrix24\ApiClientBitrix24; $apiSettings = new ApiClientSettings(); $apiSettings->setWebhookAuthEnabled(true) ->setDefaultConnection('https://portal.bitrix24.ru/rest/1/cj03r****1wbeg/'); $api = new ApiClientBitrix24($apiSettings); $result = $api->call('crm.deal.get', ['ID' => 1]);
2. Соединение OAuth 2.0 (Локальное или Тиражное приложение)
Вы можете устанавливать одно и тоже локальное приложение на разные порталы. Только следите за корректностью client_id и client_secret при установке, иначе токены не обновятся, когда их время жизни закончится, и приложение выбросит исключение.
2.1 Подготовка базы данных для хранения токенов
Используйте любую удобную базу данных:
- PostgreSQL
- MySQL
- SQLite
- SQLServer
Создайте таблицу в базе данных. Пример запроса для MySQL:
CREATE TABLE api_tokens_bitrix24( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, member_id VARCHAR(255) UNIQUE NOT NULL, access_token VARCHAR(255) NOT NULL, expires_in VARCHAR(255) NOT NULL, application_token VARCHAR(255) NOT NULL, refresh_token VARCHAR(255) NOT NULL, domain VARCHAR(255) NOT NULL, client_endpoint VARCHAR(255) NOT NULL, client_id VARCHAR(255) NOT NULL, client_secret VARCHAR(255) NOT NULL );
2.2 Создание объекта соединения OAuth 2.0
use SimpleApiBitrix24\ApiDatabaseConfig; use SimpleApiBitrix24\ApiClientSettings; use SimpleApiBitrix24\ApiClientBitrix24; $pdo = new PDO('mysql:host=172.17.0.1;port=3306;dbname=bitrix24', 'root', 'password'); // Ваши настройки подключения к базе $databaseConfig = new ApiDatabaseConfig( pdo: $pdo, tableName: 'api_tokens_bitrix24', primaryKeyColumnName: 'id', memberIdColumnName: 'member_id', accessTokenColumnName: 'access_token', expiresInColumnName: 'expires_in', applicationTokenColumnName: 'application_token', refreshTokenColumnName: 'refresh_token', domainColumnName: 'domain', clientEndpointColumnName: 'client_endpoint', clientIdColumnName: 'client_id', clientSecretColumnName: 'client_secret' ); $apiSettings = new ApiClientSettings(); $apiSettings->setTokenAuthEnabled(true) ->setDefaultConnection('your_member_id'); $api = new ApiClientBitrix24($apiSettings, $databaseConfig); $result = $api->call('crm.deal.get', ['ID' => 1]);
Если надо динамически устанавливать соединение к порталу на входящий $_REQUEST['member_id'], то делайте так:
// ... $apiSettings = new ApiClientSettings(); $apiSettings->setTokenAuthEnabled(true); $api = new ApiClientBitrix24($apiSettings, $databaseConfig); $api->connectTo($_REQUEST['member_id']); $result = $api->call('crm.deal.get', ['ID' => 1]);
2.3 Установка приложения
use SimpleApiBitrix24\Services\Installation\InstallationService; // старт установки (добавление пользователя в базу данных) $installationService = new InstallationService(); $installationService->startInstallation( 'local.67c9b****83.1668***79', // client id '7KriLM5****T6tCgVSqUj2ILZFms5*****keBzYbzqso', // client secret $databaseConfig, // SimpleApiBitrix24\ApiDatabaseConfig, пример создания объекта выше $_REQUEST ); // тут логика установки приложения, если требуется. $api->connectTo($_REQUEST['member_id']); // SimpleApiBitrix24\ApiClientBitrix24, пример создания объекта выше $result = $api->call('scope'); // завершение установки $installationService->finishInstallation(); // перезагрузка страницы на index
3. Смена соединения или клонирование объекта соединения
Смена/установка соединения
$api->connectTo('member_id__or__webhook_url');
Клонирование объекта соединения, если нужно работать одновременно с разными порталами Битрикс24.
$secondApi = clone $firstApi; // объект SimpleApiBitrix24\ApiClientBitrix24 $secondApi->connectTo('new_member_id__or__webhook_url'); // получаем второй объект с соединением к другому порталу
4. Логирование
4.1 Debug логирование
При уровне логирования DEBUG, будут логироваться все запросы и ответы.
use Monolog\Formatter\LineFormatter; use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; $logger = new Logger('api-b24'); $handler = new RotatingFileHandler( '/var/www/poject/storage/logs/api-b24.log', // ваша путь для логов 5, Logger::DEBUG ); $formatter = new LineFormatter( "[%datetime%] %level_name%: %message% %context%\n", 'Y-m-d H:i:s', true ); $formatter->setJsonPrettyPrint(true); $handler->setFormatter($formatter); $logger->pushHandler($handler); $api = new ApiClientBitrix24($apiSettings, null, $logger); // SimpleApiBitrix24\ApiClientBitrix24, пример создания объекта выше
4.2 Рекомендуемый уровень логирования
Рекомендуемый уровень логирования WARNING. В логи попадут только ответы сервера Bitrix24 с ошибками, или исключения этого пакета SimpleApiBitrix24.
// ... $handler = new RotatingFileHandler( '/var/www/poject/storage/logs/api-b24.log', // ваша путь для логов 5, Logger::WARNING ); // ...
5. Пакетные запросы
5.1 Стандартный пакетный запрос
В один пакетный запрос можно завернуть до 50 запросов.
https://apidocs.bitrix24.ru/api-reference/how-to-call-rest-api/batch.html
$result = $api->callBatch([ [ 'method' => 'crm.deal.get', 'params' => ['id' => 1] ], [ 'method' => 'tasks.deal.get', 'params' => ['id' => 2] ], ]);
5.2 Батч сервис
SimpleApiBitrix24\Services\Batch
Получение всех элементов сущности. Работает только с методами списочного типа.
use SimpleApiBitrix24\Services\Batch; $batchService = new Batch($api); // $api объект SimpleApiBitrix24\ApiClientBitrix24 $tasks = $batchService->getAll('tasks.task.list', ['filter' => ['STATUS' => 5]]);
Получение результата с учетом названий переданных ключей. Лимит: 50 упакованных запросов за один вызов.
use SimpleApiBitrix24\Services\Batch; $batchService = new Batch($api); // $api объект SimpleApiBitrix24\ApiClientBitrix24 $batchService->callWithKeys([ 'scope_response' => ['method' => 'scope', 'params' => []], 'deal_list_response' => ['method' => 'crm.deal.list', 'params' => ['select' => ['ID', 'TITLE']]], ]);
6. Встроенный сервис обработки лимитов REST API
Подробней о лимитах rest api смотрите в официальной документации
Этот пакет может обрабатывать ответы об ошибках лимитов REST API:
Пример обрабатываемых ошибок от сервера REST API:
{ "error": "QUERY_LIMIT_EXCEEDED", "error_description": "Too many requests" }
{ "error": "OPERATION_TIME_LIMIT", "error_description": "Method is blocked due to operation time limit." }
Включается обработка ошибок конфигурацией объекта SimpleApiBitrix24\ApiClientSettings
use SimpleApiBitrix24\ApiClientSettings; $apiSettings = new ApiClientSettings(); $apiSettings->setTokenAuthEnabled(true) ->setDefaultConnection('your_member_id') ->setQueryLimitExceededService(handleEnabled: true, usleep: 500000) ->setOperationTimeLimitService(handleEnabled: true, usleep: 5000000);
По умолчанию обработка этих ошибок отключена, при включении укажите время ожидания в микросекундах. При получении ответа с одной из этих ошибок API Client будет делать повторный запрос через заданный интервал времени, не останавливая работу скрипта, делать это будет постоянно пока не завершится выполнение скрипта или не закончится время жизни приложения.
English
Client for Bitrix24 REST API:
OAuth 2.0 (with automatic token refresh), Webhook, support for all popular relational databases, manager for installing local/distributed applications. Installation of a single local application across multiple portals.
Table of Contents:
-
OAuth 2.0 Connection (Local or Edition App))
2.1. Preparing the Database for Token Storage
2.2. Creating an OAuth 2.0 Connection Object
2.3. App Installation
-
4.1. Debug Logging
-
5.2 Batch Service
1. Quick Start: Webhook Connection
use SimpleApiBitrix24\ApiClientSettings; use SimpleApiBitrix24\ApiClientBitrix24; $apiSettings = new ApiClientSettings(); $apiSettings->setWebhookAuthEnabled(true) ->setDefaultConnection('https://portal.bitrix24.ru/rest/1/cj03r****1wbeg/'); $api = new ApiClientBitrix24($apiSettings); $result = $api->call('crm.deal.get', ['ID' => 1]);
2. OAuth 2.0 Connection (Local or Edition App
You can install the same local app on different portals. Ensure client_id and client_secret are correct during installation, otherwise tokens won’t refresh when they expire, and the app will throw an exception.
2.1 Preparing the Database for Token Storage
Use any database of your choice:
- PostgreSQL
- MySQL
- SQLite
- SQLServer
Create a table in the database. Example query for MySQL:
CREATE TABLE api_tokens_bitrix24( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, member_id VARCHAR(255) UNIQUE NOT NULL, access_token VARCHAR(255) NOT NULL, expires_in VARCHAR(255) NOT NULL, application_token VARCHAR(255) NOT NULL, refresh_token VARCHAR(255) NOT NULL, domain VARCHAR(255) NOT NULL, client_endpoint VARCHAR(255) NOT NULL, client_id VARCHAR(255) NOT NULL, client_secret VARCHAR(255) NOT NULL );
2.2 Creating an OAuth 2.0 Connection Object
use SimpleApiBitrix24\ApiDatabaseConfig; use SimpleApiBitrix24\ApiClientSettings; use SimpleApiBitrix24\ApiClientBitrix24; $pdo = new PDO('mysql:host=172.17.0.1;port=3306;dbname=bitrix24', 'root', 'password'); // Your database connection settings $databaseConfig = new ApiDatabaseConfig( pdo: $pdo, tableName: 'api_tokens_bitrix24', primaryKeyColumnName: 'id', memberIdColumnName: 'member_id', accessTokenColumnName: 'access_token', expiresInColumnName: 'expires_in', applicationTokenColumnName: 'application_token', refreshTokenColumnName: 'refresh_token', domainColumnName: 'domain', clientEndpointColumnName: 'client_endpoint', clientIdColumnName: 'client_id', clientSecretColumnName: 'client_secret' ); $apiSettings = new ApiClientSettings(); $apiSettings->setTokenAuthEnabled(true) ->setDefaultConnection('your_member_id'); $api = new ApiClientBitrix24($apiSettings, $databaseConfig); $result = $api->call('crm.deal.get', ['ID' => 1]);
To dynamically set the connection based on $_REQUEST['member_id'], do this:
// ... $apiSettings = new ApiClientSettings(); $apiSettings->setTokenAuthEnabled(true); $api = new ApiClientBitrix24($apiSettings, $databaseConfig); $api->connectTo($_REQUEST['member_id']); $result = $api->call('crm.deal.get', ['ID' => 1]);
2.3 App Installation
use SimpleApiBitrix24\Services\Installation\InstallationService; // Start installation (add user to the database) $installationService = new InstallationService(); $installationService->startInstallation( 'local.67c9b****83.1668***79', // client id '7KriLM5****T6tCgVSqUj2ILZFms5*****keBzYbzqso', // client secret $databaseConfig, // SimpleApiBitrix24\ApiDatabaseConfig, see creation example above $_REQUEST ); // Add your app installation logic here, if needed $api->connectTo($_REQUEST['member_id']); // SimpleApiBitrix24\ApiClientBitrix24, see creation example above $result = $api->call('scope'); // Finish installation $installationService->finishInstallation(); // Reloads the page to index
3. Switching or Cloning the Connection
Switching/setting a connection:
$api->connectTo('member_id__or__webhook_url'); // SimpleApiBitrix24\ApiClientBitrix24 object
Cloning the connection object to work with multiple Bitrix24 portals simultaneously:
$secondApi = clone $firstApi; // SimpleApiBitrix24\ApiClientBitrix24 object $secondApi->connectTo('new_member_id__or__webhook_url'); // Creates a second object connected to another portal
4. Logging
4.1 Debug Logging
At the DEBUG logging level, all requests and responses will be logged.
use Monolog\Formatter\LineFormatter; use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; $logger = new Logger('api-b24'); $handler = new RotatingFileHandler( '/var/www/poject/storage/logs/api-b24.log', // Your log file path 5, Logger::DEBUG ); $formatter = new LineFormatter( "[%datetime%] %level_name%: %message% %context%\n", 'Y-m-d H:i:s', true ); $formatter->setJsonPrettyPrint(true); $handler->setFormatter($formatter); $logger->pushHandler($handler); $api = new ApiClientBitrix24($apiSettings, null, $logger); // SimpleApiBitrix24\ApiClientBitrix24, see creation example above
4.2 Recommended Logging Level
The recommended logging level is WARNING.
Only Bitrix24 server error responses or exceptions from this SimpleApiBitrix24 package will be logged.
// ... $handler = new RotatingFileHandler( '/var/www/poject/storage/logs/api-b24.log', // Your log file path 5, Logger::WARNING ); // ...
5. Batch Requests
5.1 Standard Batch Request
Up to 50 requests can be included in a single batch request.
https://apidocs.bitrix24.com/api-reference/how-to-call-rest-api/batch.html
$result = $api->callBatch([ [ 'method' => 'crm.deal.get', 'params' => ['id' => 1] ], [ 'method' => 'tasks.deal.get', 'params' => ['id' => 2] ], ]);
5.2 Batch Service
SimpleApiBitrix24\Services\Batch
Retrieving all entity items. Works only with list-type methods.
use SimpleApiBitrix24\Services\Batch; $batchService = new Batch($api); // $api is an instance of SimpleApiBitrix24\ApiClientBitrix24 $tasks = $batchService->getAll('tasks.task.list', ['filter' => ['STATUS' => 5]]);
Retrieving results with specified key names. Limit: 50 batched requests per call.
use SimpleApiBitrix24\Services\Batch; $batchService = new Batch($api); // $api is an instance of SimpleApiBitrix24\ApiClientBitrix24 $batchService->callWithKeys([ 'scope_response' => ['method' => 'scope', 'params' => []], 'deal_list_response' => ['method' => 'crm.deal.list', 'params' => ['select' => ['ID', 'TITLE']]], ]);
6. Built-in REST API Limit Handling Service
For more details on REST API limits, refer to the official documentation:
This package can handle REST API limit error responses:
Examples of handled REST API server errors:
{ "error": "QUERY_LIMIT_EXCEEDED", "error_description": "Too many requests" }
{ "error": "OPERATION_TIME_LIMIT", "error_description": "Method is blocked due to operation time limit." }
Error handling is enabled through the configuration of the SimpleApiBitrix24\ApiClientSettings object:
use SimpleApiBitrix24\ApiClientSettings; $apiSettings = new ApiClientSettings(); $apiSettings->setTokenAuthEnabled(true) ->setDefaultConnection('your_member_id') ->setQueryLimitExceededService(handleEnabled: true, usleep: 500000) ->setOperationTimeLimitService(handleEnabled: true, usleep: 5000000);
By default, handling of these errors is disabled. When enabled, specify the wait time in microseconds. Upon receiving one of these error responses, the API client will retry the request after the specified time interval without stopping the script's execution. It will continue doing so until the script completes or the application's lifetime expires.