mesilov / bitrix24-php-sdk
A powerful PHP library for the Bitrix24 REST API
Fund package maintenance!
boosty.to/bitrix24-php-sdk
Installs: 177 355
Dependents: 6
Suggesters: 0
Security: 0
Stars: 409
Watchers: 46
Forks: 162
Open Issues: 23
Requires
- php: 8.2.* || 8.3.*
- ext-curl: *
- ext-intl: *
- ext-json: *
- darsyn/ip: ^4 || ^5
- fig/http-message-util: ^1
- giggsey/libphonenumber-for-php: ^8
- mesilov/moneyphp-percentage: ^0.2
- moneyphp/money: ^3 || ^4
- nesbot/carbon: ^3
- psr/log: ^2 || ^3
- symfony/console: ^6 || ^7
- symfony/dotenv: ^6 || ^7
- symfony/event-dispatcher: ^6 || ^7
- symfony/filesystem: ^6 || ^7
- symfony/finder: ^6 || ^7
- symfony/http-client: ^6 || ^7
- symfony/http-client-contracts: ^2 || ^3
- symfony/http-foundation: ^6 || ^7
- symfony/mime: ^6 || ^7
- symfony/uid: ^6 || ^7
Requires (Dev)
- fakerphp/faker: ^1
- monolog/monolog: ^3
- nunomaduro/phpinsights: ^2
- phpstan/phpstan: 1.11.7
- phpunit/phpunit: ^10 || ^11
- psalm/phar: ^5
- rector/rector: ^1
- roave/security-advisories: dev-master
- symfony/debug-bundle: ^6 || ^7
- symfony/stopwatch: ^6 || ^7
- typhoon/reflection: ^0.4
- dev-master
- 2.x-dev
- 2.0
- 2.0-beta.3
- 2.0-beta.2
- 2.0-beta.1
- 2.0-alpha.7
- 2.0-alpha.6
- 2.0-alpha.5
- 2.0-alpha.4
- 2.0-alpha.3
- 1.x-dev
- 1.3
- 1.2.2
- 1.2.0
- 1.1.0
- 1.0.0
- 0.7.0
- 0.6.1
- 0.6.0
- 0.4.0
- 0.2.0
- dev-385-publish-b24-php-sdk-beta-3
- dev-feature/390-prepare-publish-2-0
- dev-feature/383-refactor-app-contracts-and-add-documentation-for-use-cases
- dev-dev
- dev-306-beta-1
- dev-225-composite-batch
This package is auto-updated.
Last update: 2024-11-28 18:41:05 UTC
README
A powerful PHP library for the Bitrix24 REST API
Build status
Integration tests run in GitHub actions with real Bitrix24 portal
BITRIX24-PHP-SDK ✨FEATURES✨
Support both auth modes:
- work with auth tokens for Bitrix24 applications in marketplace
- work with incoming webhooks for simple integration projects for current portal
Domain core events:
- Access Token expired
- Bitrix24 portal domain url changed
API - level features
- Auto renew access tokens
- List queries with «start=-1» support
- offline queues
Performance improvements 🚀
- Batch queries implemented with PHP Generators – constant low memory and low CPI usage:
- batch read data from bitrix24
- batch write data to bitrix24
- read without count flag
Development principles
- Good developer experience
- auto-completion of methods at the IDE
- typed method call signatures
- typed results of method calls
- helpers for typical operations
- Good documentation
- documentation on the operation of a specific method containing a link to the official documentation
- documentation for working with the SDK
- Performance first:
- minimal impact on client code
- ability to work with large amounts of data with constant memory consumption
- efficient operation of the API using batch requests
- Modern technology stack
- based on Symfony HttpClient
- actual PHP versions language features
- Reliable:
- test coverage: unit, integration, contract
- typical examples typical for different modes of operation and they are optimized for memory \ performance
Architecture
Abstraction layers
- http2 protocol via json data structures
- symfony http client
- \Bitrix24\SDK\Core\ApiClient - work with b24 rest-api endpoints
input: arrays \ strings
output: Symfony\Contracts\HttpClient\ResponseInterface, operate with strings
process: network operations
- \Bitrix24\SDK\Services\* - work with b24 rest-api entities
input: arrays \ strings
output: b24 response dto
process: b24 entities, operate with immutable objects
Documentation
- Bitrix24 API documentation - English
- Internal documentation for bitrix24-php-sdk
Requirements
- php: >=8.2
- ext-json: *
- ext-curl: *
Installation
Add "mesilov/bitrix24-php-sdk": "2.x"
to composer.json
of your application. Or clone repo to your project.
Examples
Work with webhook
- Go to
/examples/webhook
folder - Open console and install dependencies
composer install
- Open Bitrix24 portal: Developer resources → Other → Inbound webhook
- Open example file and insert webhook url into
$webhookUrl
see example.php file
declare(strict_types=1); use Bitrix24\SDK\Services\ServiceBuilderFactory; use Symfony\Component\EventDispatcher\EventDispatcher; use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Processor\MemoryUsageProcessor; require_once 'vendor/autoload.php'; $webhookUrl = 'INSERT_HERE_YOUR_WEBHOOK_URL'; $log = new Logger('bitrix24-php-sdk'); $log->pushHandler(new StreamHandler('bitrix24-php-sdk.log')); $log->pushProcessor(new MemoryUsageProcessor(true, true)); // create service builder factory $b24ServiceFactory = new ServiceBuilderFactory(new EventDispatcher(), $log); // init bitrix24-php-sdk service from webhook $b24Service = $b24ServiceFactory->initFromWebhook($webhookUrl); // work with interested scope var_dump($b24Service->getMainScope()->main()->getCurrentUserProfile()->getUserProfile()); // get deals list and address to first element var_dump($b24Service->getCRMScope()->lead()->list([], [], ['ID', 'TITLE'])->getLeads()[0]->TITLE);
- Call php file in shell
php -f example.php
Work with local application
- Go to
/examples/local-application
folder - Open console and install dependencies
composer install
- Start local development server
sudo php -S 127.0.0.1:80
- Expose local server to public via ngrok and remember temporally public url –
https://****.ngrok-free.app
ngrok http 127.0.0.1
- Check public url from ngrok and see
x-powered-by
header with 200 status-code.
curl https://****.ngrok-free.app -I HTTP/2 200 content-type: text/html; charset=UTF-8 date: Mon, 26 Aug 2024 19:09:24 GMT host: ****.ngrok-free.app x-powered-by: PHP/8.3.8
- Open Bitrix24 portal: Developer resources → Other → Local application and create new local application:
type
: serverhandler path
:https://****.ngrok-free.app/index.php
Initial installation path
:https://****.ngrok-free.app/install.php
Menu item text
:Test local app
scope
:crm
- Save application parameters in
index.php
file:Application ID (client_id)
—BITRIX24_PHP_SDK_APPLICATION_CLIENT_ID
Application key (client_secret)
—BITRIX24_PHP_SDK_APPLICATION_CLIENT_SECRET
Assing permitions (scope)
—BITRIX24_PHP_SDK_APPLICATION_SCOPE
see index.php file
declare(strict_types=1); use Bitrix24\SDK\Core\Credentials\AuthToken; use Bitrix24\SDK\Core\Credentials\ApplicationProfile; use Bitrix24\SDK\Services\ServiceBuilderFactory; use Monolog\Handler\StreamHandler; use Monolog\Logger; use Monolog\Processor\MemoryUsageProcessor; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; require_once 'vendor/autoload.php'; ?> <pre> Application is worked, auth tokens from bitrix24: <?= print_r($_REQUEST, true) ?> </pre> <?php $request = Request::createFromGlobals(); $log = new Logger('bitrix24-php-sdk'); $log->pushHandler(new StreamHandler('bitrix24-php-sdk.log')); $log->pushProcessor(new MemoryUsageProcessor(true, true)); $b24ServiceBuilderFactory = new ServiceBuilderFactory(new EventDispatcher(), $log); $appProfile = ApplicationProfile::initFromArray([ 'BITRIX24_PHP_SDK_APPLICATION_CLIENT_ID' => 'INSERT_HERE_YOUR_DATA', 'BITRIX24_PHP_SDK_APPLICATION_CLIENT_SECRET' => 'INSERT_HERE_YOUR_DATA', 'BITRIX24_PHP_SDK_APPLICATION_SCOPE' => 'INSERT_HERE_YOUR_DATA' ]); $b24Service = $b24ServiceBuilderFactory->initFromRequest($appProfile, AuthToken::initFromPlacementRequest($request), $request->get('DOMAIN')); var_dump($b24Service->getMainScope()->main()->getCurrentUserProfile()->getUserProfile()); // get deals list and address to first element var_dump($b24Service->getCRMScope()->lead()->list([], [], ['ID', 'TITLE'])->getLeads()[0]->TITLE);
Create application for Bitrix24 marketplace
if you want to create application you can use production-ready contracts in namespace
Bitrix24\SDK\Application\Contracts
:
Bitrix24Accounts
— Store auth tokens and provides methods for work with Bitrix24 account.ApplicationInstallations
— Store information about application installation, linked with Bitrix24 Account with auth tokens. Optional can store links to:- Client contact person: client person who responsible for application usage
- Bitrix24 Partner contact person: partner contact person who supports client and configure application
- Bitrix24 Partner: partner who supports client portal
ContactPersons
– Store information about person who installed application.Bitrix24Partners
– Store information about Bitrix24 Partner who supports client portal and install or configure application.
Steps:
- Create own entity of this bounded contexts.
- Implement all methods in contract interfaces.
- Test own implementation behavior with contract-tests
tests/Unit/Application/Contracts/*
– examples.
Tests
Tests locate in folder tests
and we have two test types.
In folder tests create file .env.local
and fill environment variables from .env
.
PHP Static Analysis Tool – phpstan
Call in command line
make lint-phpstan
PHP Static Analysis Tool – rector
Call in command line for validate
make lint-rector
Call in command line for fix codebase
make lint-rector-fix
Unit tests
Fast, in-memory tests without a network I\O For run unit tests you must call in command line
make test-unit
Integration tests
Slow tests with full lifecycle with your test Bitrix24 portal via webhook.
❗️Do not run integration tests with production portals
For run integration test you must:
- Create new Bitrix24 portal for development tests.
- Go to left menu, click «Sitemap».
- Find menu item «Developer resources».
- Click on menu item «Other».
- Click on menu item «Inbound webhook».
- Assign all permisions with webhook and click «save» button.
- Create file
/tests/.env.local
with same settings, see comments in/tests/.env
file.
APP_ENV=dev BITRIX24_WEBHOOK=https:// your portal webhook url INTEGRATION_TEST_LOG_LEVEL=500
- call in command line
make test-integration-core make test-integration-scope-telephony make test-integration-scope-workflows make test-integration-scope-user
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub
License
bitrix24-php-sdk is licensed under the MIT License - see the MIT-LICENSE.txt
file for details
Authors
Maksim Mesilov - mesilov.maxim@gmail.com
See also the list of contributors which participated in this project.
Sponsors
Need custom Bitrix24 application?
Email to mesilov.maxim@gmail.com for private consultations or dedicated support.