bitrix24/b24phpsdk
Official PHP SDK for the Bitrix24 REST API. Supports v1 (stable, PHP 8.2–8.4) and v3 (new endpoints, PHP 8.4+, breaking changes). Provides typed clients for Bitrix24 REST methods with active CI and production-ready releases.
Install the SDK (choose version based on PHP compatibility):
composer require bitrix24/b24phpsdk:"^1.0" # v1 (PHP 8.2-8.4)
composer require bitrix24/b24phpsdk:"^3.2" # v3 (PHP 8.4+, breaking changes)
Initialize the service (pick one auth method):
use Bitrix24\SDK\Services\ServiceBuilderFactory;
$b24Service = ServiceBuilderFactory::createServiceBuilderFromWebhook('YOUR_WEBHOOK_URL');
use Bitrix24\SDK\Core\Credentials\ApplicationProfile;
$appProfile = ApplicationProfile::initFromArray([
'CLIENT_ID' => 'your_client_id',
'CLIENT_SECRET' => 'your_secret',
'SCOPE' => 'crm,im' // Comma-separated permissions
]);
$b24Service = ServiceBuilderFactory::createServiceBuilderFromPlacementRequest(
\Symfony\Component\HttpFoundation\Request::createFromGlobals(),
$appProfile
);
First API call (e.g., fetch CRM deals):
$deals = $b24Service->getMainScope()->crm()->deals()->getDeals();
/src/Services/ – Service classes (e.g., CrmService, ImService)./examples/ – Ready-to-use workflows (webhook, local app, marketplace)./tests/ – Integration patterns (use .env.local for test credentials).Use builder methods to chain operations:
// v3 example (v1 uses similar but shorter paths)
$deals = $b24Service
->getMainScope()
->crm()
->deals()
->getDeals(['SELECT' => ['ID', 'TITLE', 'AMOUNT']]);
Batch Operations (memory-efficient for large datasets):
foreach ($b24Service->getMainScope()->crm()->deals()->getDealsBatch() as $deal) {
// Process one deal at a time (generator)
yield $deal->getId();
}
401 errors (configure via ApiClient).$webhookSecret = 'your_secret';
$isValid = $b24Service->getCore()->validateWebhookRequest(
$_SERVER['HTTP_X_B24_WEBHOOK_SIGNATURE'],
file_get_contents('php://input'),
$webhookSecret
);
Create:
$deal = $b24Service->getMainScope()->crm()->deals()->addDeal([
'TITLE' => 'New Deal',
'AMOUNT' => 1000
]);
Update:
$deal->setTitle('Updated Deal')->update();
Delete:
$deal->delete();
Listen for webhook events (e.g., CRM deal updates):
$event = $b24Service->getCore()->getWebhookEvent();
switch ($event->getType()) {
case 'crm.deal.add':
$deal = $event->getData()['fields'];
break;
// Handle other events...
}
Service Provider:
// app/Providers/Bitrix24ServiceProvider.php
public function register()
{
$this->app->singleton('bitrix24', function () {
return ServiceBuilderFactory::createServiceBuilderFromWebhook(
config('services.bitrix24.webhook_url')
);
});
}
Usage in Controllers:
public function syncDeals()
{
$deals = app('bitrix24')->getMainScope()->crm()->deals()->getDeals();
// Process deals...
}
Wrap API calls in try-catch:
try {
$result = $b24Service->core()->call('crm.deal.get', ['ID' => 1]);
} catch (\Bitrix24\SDK\Core\Exceptions\ApiException $e) {
if ($e->getCode() === 401) {
// Token expired; SDK auto-renews, but handle manually if needed
}
Log::error($e->getMessage());
}
/rest/api/ endpoints (v1: /rest/).getDeals() → getDealsBatch()).crm, im).getDeals() without limits; use getDealsBatch() or SELECT filters.$deals = $b24Service->getMainScope()->crm()->deals()->getDeals([
'SELECT' => ['ID', 'TITLE'],
'FILTER' => ['AMOUNT' => ['>=', 1000]],
'START' => 0,
'TOP' => 100
]);
$b24Service->getMainScope()->crm()->deals()->addDealsBatch([
['TITLE' => 'Deal 1', 'AMOUNT' => 100],
['TITLE' => 'Deal 2', 'AMOUNT' => 200]
]);
COUNT => false to reduce payload size for list queries.\Bitrix24\SDK\Services\AbstractService to wrap undocumented endpoints:
class CustomService extends AbstractService {
public function customMethod(array $params): array {
return $this->getApiClient()->call('custom.endpoint', $params);
}
}
ApiClient:
$client = new ApiClient(
$webhookUrl,
new \Symfony\Component\HttpClient\CurlHttpClient(),
new \Bitrix24\SDK\Core\Middleware\LoggingMiddleware()
);
.env.local to override test credentials (never commit this file).--filter to target specific scopes:
make test-integration-scope-crm
ApiClientMock for unit tests:
$mockClient = new ApiClientMock();
$mockClient->setResponse('crm.deal.get', ['result' => ['ID' => 1]]);
$service = new CrmService($mockClient);
BITRIX24_WEBHOOK_SECRET).ngrok for local development (as shown in examples)./install.php endpoint to handle OAuth callbacks.Ctrl+Click on service methods to jump to the [Bitrix24 API docs](https://apidocs.bitrix24.com/How can I help you explore Laravel packages today?