PHP code example of reutskiy-a / simple-api-bitrix24

1. Go to this page and download the library: Download reutskiy-a/simple-api-bitrix24 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reutskiy-a / simple-api-bitrix24 example snippets


    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]);

    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]);

    // ...
    
    $apiSettings = new ApiClientSettings();
    $apiSettings->setTokenAuthEnabled(true);
    
    $api = new ApiClientBitrix24($apiSettings, $databaseConfig);
    $api->connectTo($_REQUEST['member_id']);
    
    $result = $api->call('crm.deal.get', ['ID' => 1]);

    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

    $api->connectTo('member_id__or__webhook_url');

    $secondApi = clone $firstApi;                                   // объект SimpleApiBitrix24\ApiClientBitrix24
    $secondApi->connectTo('new_member_id__or__webhook_url');        // получаем второй объект с соединением к другому порталу

    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
    );
    $handler->setFormatter($formatter);
    $logger->pushHandler($handler);

    $api = new ApiClientBitrix24($apiSettings, null, $logger);          // SimpleApiBitrix24\ApiClientBitrix24, пример создания объекта выше

    // ...
    
    $handler = new RotatingFileHandler(
        '/var/www/poject/storage/logs/api-b24.log',                     // ваша путь для логов
        5,
    Logger::WARNING
    );
    
    // ...


    $result = $api->callBatch([
        [
            'method' => 'crm.deal.get',
            'params' => ['id' => 1]
        ],
        [
            'method' => 'tasks.deal.get',
            'params' => ['id' => 2]
        ],
    ]);

    use SimpleApiBitrix24\Services\Batch;
    
    $batchService = new Batch($api);                   // $api объект SimpleApiBitrix24\ApiClientBitrix24
    $tasks = $batchService->getAll('tasks.task.list', ['filter' => ['STATUS' => 5]]);

    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]);

    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]);

    // ...
    
    $apiSettings = new ApiClientSettings();
    $apiSettings->setTokenAuthEnabled(true);
    
    $api = new ApiClientBitrix24($apiSettings, $databaseConfig);
    $api->connectTo($_REQUEST['member_id']);
    
    $result = $api->call('crm.deal.get', ['ID' => 1]);

    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

    $api->connectTo('member_id__or__webhook_url');                  // SimpleApiBitrix24\ApiClientBitrix24 object

    $secondApi = clone $firstApi;                                   // SimpleApiBitrix24\ApiClientBitrix24 object
    $secondApi->connectTo('new_member_id__or__webhook_url');        // Creates a second object connected to another portal

    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
    );
    $handler->setFormatter($formatter);
    $logger->pushHandler($handler);

    $api = new ApiClientBitrix24($apiSettings, null, $logger);          // SimpleApiBitrix24\ApiClientBitrix24, see creation example above

    // ...
    
    $handler = new RotatingFileHandler(
        '/var/www/poject/storage/logs/api-b24.log',                     // Your log file path
        5,
    Logger::WARNING
    );
    
    // ...


    $result = $api->callBatch([
        [
            'method' => 'crm.deal.get',
            'params' => ['id' => 1]
        ],
        [
            'method' => 'tasks.deal.get',
            'params' => ['id' => 2]
        ],
    ]);

    use SimpleApiBitrix24\Services\Batch;
    
    $batchService = new Batch($api);                   // $api is an instance of SimpleApiBitrix24\ApiClientBitrix24
    $tasks = $batchService->getAll('tasks.task.list', ['filter' => ['STATUS' => 5]]);