How bitrix-tools/crest works
Project Type and Purpose
• This is an open-source PHP SDK (PHPSDK) for interacting with the Bitrix24 REST API. It provides a small library (CRest) plus example scripts (index.php, install.php, checkserver.php) and a configuration file (settings.php). It can be used as:
• A webhook client (inbound webhook)
• A local or public OAuth2-based application
Main Components
A. Client (Browser or CLI)
• Triggers HTTP requests to your server endpoints (index.php, install.php, checkserver.php).
B. Web Server / PHP Runtime
• Hosts the PHP scripts. Must have cURL extension and valid SSL certificate.
C. PHP Scripts (src folder & root)
• src/crest.php: Defines the CRest class (core SDK).
• index.php: Example usage entrypoint to call CRest::call().
• install.php: Performs OAuth2 install flows for local or public apps.
• checkserver.php: Verifies server cURL and SSL readiness.
• settings.php: Configuration (webhook URL, OAuth client_id/secret, encoding constant, and persistence methods).
D. Persistence Layer
• Default: text file via CRest::getSettingData/setSettingData.
• Customizable by inheriting CRest and overriding those methods to use DB or other storage.
E. External Service
• Bitrix24 REST API endpoints (over HTTPS).
Relationships & Data Flows
1. Browser → HTTP GET/POST → index.php / install.php / checkserver.php
2. PHP script loads settings.php and crest.php (CRest class)
3. CRest::call(...) constructs an HTTP request via PHP cURL
4. cURL → HTTPS request → Bitrix24 REST API
5. Bitrix24 responds → cURL → CRest parses JSON → returns to PHP script
6. PHP script echoes JSON result to browser
7. For OAuth2 flows:
a. install.php redirects user to Bitrix24 OAuth2 consent URL
b. Bitrix24 redirects back with code → install.php exchanges code for tokens
c. CRest::setSettingData persists access/refresh tokens
Architectural Patterns & Design Principles
• Single-responsibility: separate scripts for example usage (index), installation (install), health check (checkserver).
• Pluggable persistence: settings.php holds constants; CRest allows overriding getSettingData/setSettingData for custom token storage.
• External integration: CRest acts as an adapter/facade over the Bitrix24 REST API using cURL.