bitrix24/b24phpsdk
Bitrix24 PHP SDK for working with the Bitrix24 REST API from Laravel or plain PHP. Provides typed clients, authentication helpers, API method wrappers, pagination, and webhook/OAuth support to simplify integrating CRM, tasks, chats, and other Bitrix24 modules.
composer require bitrix24/b24phpsdk
config/services.php (or a dedicated b24.php config file):
'b24' => [
'client_id' => env('B24_CLIENT_ID'),
'client_secret' => env('B24_CLIENT_SECRET'),
'redirect_uri' => env('B24_REDIRECT_URI'),
'domain' => env('B24_DOMAIN', 'yourdomain.bitrix24.com'),
'token' => env('B24_ACCESS_TOKEN'),
],
php artisan vendor:publish --provider="Bitrix24\B24PhpSdk\B24PhpSdkServiceProvider"
use Bitrix24\B24PhpSdk\B24Client;
$client = new B24Client(config('b24'));
$contact = $client->crm()->contacts()->get(123); // Fetch contact with ID 123
new B24Client(config('b24'))$client->crm()->[entity]()->[method]()$client->auth()->getAuthUrl() (for OAuth)Wrap the SDK in a Laravel service class to abstract Bitrix24 logic:
namespace App\Services;
use Bitrix24\B24PhpSdk\B24Client;
class Bitrix24Service
{
protected $client;
public function __construct()
{
$this->client = new B24Client(config('b24'));
}
public function createLead(array $data)
{
return $this->client->crm()->leads()->add($data);
}
}
Usage in Controller:
public function store(Request $request)
{
$lead = app(Bitrix24Service::class)->createLead($request->all());
return response()->json($lead);
}
Handle authorization with a redirect:
// Generate auth URL
$authUrl = app(B24Client::class)->auth()->getAuthUrl();
// After redirect, exchange code for token
$token = app(Bitrix24Service::class)->client->auth()->getAccessToken($code);
Use collections for batch processing:
$contacts = $client->crm()->contacts()->getList(['SELECT' => ['ID', 'NAME']]);
foreach ($contacts as $contact) {
// Process each contact
}
Extend the SDK to handle Bitrix24 webhooks:
use Bitrix24\B24PhpSdk\Events\WebhookEvent;
class Bitrix24WebhookHandler
{
public function handle(WebhookEvent $event)
{
if ($event->type === 'lead.add') {
// Handle new lead
}
}
}
Extend the SDK for unsupported endpoints:
use Bitrix24\B24PhpSdk\B24Client;
$client = new B24Client(config('b24'));
$response = $client->request('GET', '/rest/1/yourcustomendpoint');
Token Expiry:
Bitrix24\B24PhpSdk\Exceptions\TokenExpiredException.public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (TokenExpiredException $e) {
$token = $this->refreshToken();
config(['b24.token' => $token]);
return $next($request);
}
}
Rate Limiting:
$cacheKey = 'b24_contacts_' . $id;
return Cache::remember($cacheKey, now()->addMinutes(5), function () use ($client, $id) {
return $client->crm()->contacts()->get($id);
});
Field Mappings:
UF_* for custom fields. Ensure your SDK version supports them:
$lead = $client->crm()->leads()->add([
'TITLE' => 'New Lead',
'UF_CRM_123456789' => 'Custom Value', // Custom field
]);
Enable Debug Mode:
$client = new B24Client(config('b24'), [
'debug' => true,
]);
storage/logs/b24.log.Validate Responses:
->validate() on responses to check for errors:
$response = $client->crm()->contacts()->get(123)->validate();
Test with Sandbox:
https://sandbox.bitrix24.com for testing before going live.Custom Entities:
Bitrix24\B24PhpSdk\Entities\Entity for new CRM objects:
class CustomEntity extends Entity
{
protected $endpoint = 'customentity';
}
Middleware:
$client->addMiddleware(function ($request) {
$request->headers->set('X-Custom-Header', 'value');
});
Event Dispatching:
event(new WebhookEvent($data));
->batch() for multiple operations:
$client->crm()->contacts()->batch([
['method' => 'add', 'params' => [...]],
['method' => 'update', 'params' => [...]],
]);
->getList() with SELECT to fetch only needed fields.B24_DOMAIN is in subdomain.bitrix24.com format (not https://...).$client = new B24Client(config('b24'), [
'verify_ssl' => false,
]);
How can I help you explore Laravel packages today?