Documentation

Using Examples

REST API allows accomplishing many objectives in integrating external sources and systems with Bitrix24, as well as transferring data to Bitrix24 and etc. Here you can find some examples of most frequent cases that you can use as completed pieces of code to be modified for your own goals.

All the provided examples are created in PHP, that's why you can embed such code at the available server and must comply with the following technical requirements:

  1. Provided examples use the cURL module to execute REST queries. Find out, how to connect the cURL module on your server.
  2. Your web server must have a valid SSL certificate installed.
  3. Examples use the basic SDK Version is indicated at the start of the crest.php file: @version 1.1, 1.2 ... 1.10. It's recommended to periodically download the file and check availability of a new SDK version as a CRest class for executing queries and extending authorization tokens. Take class code, implement required corrections in the settings.php, associated with authorization file and allocate it on your server, by embedding the needed case example from documentation.
  4. When any errors occur with SDK operation, you can open the file checkserver.php in the browser and this file will performs minimal verification of server settings for CRest class operation.

When the project uses CRest class and encoding differs from utf8, 2 more additional actions are required:

  1. Open file from the archive and change the encoding to the required one.
  2. Declare the constant C_REST_CURRENT_ENCODING in the settings.php file. Foe example, when the project encoding is windows-1251, the constant must look like as follow: define('C_REST_CURRENT_ENCODING','windows-1251');.

Variant 1. REST call using an inbound webhook

  1. Specify webhook URL in define C_REST_WEB_HOOK_URL inside the settings.php file:
    <?
    define('C_REST_WEB_HOOK_URL','https://xxx.bitrix24.com/rest/1/douasdqkjxgc3mgc1/');
    
  2. Insert text of the example into the file index.php:
    <?
    require_once('crest.php');
    
    // put an example below
    echo '<PRE>';
    print_r(CRest::call(
       'crm.lead.add',
       [
          'fields' =>[
          'TITLE' => 'Lead Name',//Title*[string]
          'NAME' => 'Name',//Name[string]
          'LAST_NAME' => 'Last name',//Last name[string]
          ]
       ])
    );
    
    echo '</PRE>';
  3. Specify URL for the example in the browser address string https://mydomain.xxx/index.php, to see the results of the example.

Variant 2. REST call from the local app

  1. Inside the file settings.php:
    <?
    define('C_REST_CLIENT_ID','app.87816165165.35984727');
    define('C_REST_CLIENT_SECRET','g5dlcj3d2772h8g5jhzou907jojage');
  2. Insert text of example into the file index.php:
    <?
    require_once('crest.php');
    
    // put an example below
    echo '<PRE>';
    print_r(CRest::call(
       'crm.lead.add',
       [
          'fields' =>[
          'TITLE' => 'Lead Name',//Title*[string]
          'NAME' => 'Name',//Name[string]
          'LAST_NAME' => 'Last name',/?Last name[string]
          ]
       ])
    );
    
    echo '</PRE>';
  3. Indicate your app's URL (https://mydomain.xxx/index.php) in the details of the local application as well as an installation script https://mydomain.xxx/install.php.
  4. Indicate the parameter values client_id and client_secret inside the file settings.php for OAuth 2.0 authorization in define C_REST_CLIENT_ID and C_REST_CLIENT_SECRET, by taking these values from the local app details.
    <?
    require_once('crest.php');
    
    // put an example below
    echo '<PRE>';
    print_r(CRest::call(
       'crm.lead.add',
       [
          'fields' =>[
          'TITLE' => 'Lead Name',//Title*[string]
          'NAME' => 'Name',//Name[string]
          'LAST_NAME' => 'Last name',/?Last name[string]
          ]
       ])
    );
    
    echo '</PRE>';
  5. In the list of local apps, click left mouse button at your app to open a drop-down menu and select "Reinstall". It is needed for install.php to operate correctly after you have inserted correct values C_REST_CLIENT_ID and C_REST_CLIENT_SECRET.
  6. After installation is complete, you will see results of the example. If the example demonstrates inbuilt widgets into other Bitrix24 tools, go to these tools.

Variant 3. REST call from the standard applications

  1. Inside the file settings.php:
    <?
    define('C_REST_CLIENT_ID','app.87816165165.35984727');
    define('C_REST_CLIENT_SECRET','g5dlcj3d2772h8g5jhzou907jojage');
  2. Insert the example text into the file index.php
    <?
    require_once('crest.php');
    
    // put an example below
    echo '<PRE>';
    print_r(CRest::call(
       'crm.lead.add',
       [
          'fields' =>[
          'TITLE' => 'Lead Name',//Title*[string]
          'NAME' => 'Name',//Name[string]
          'LAST_NAME' => 'Last name',/?Last name[string]
          ]
       ])
    );
    
    echo '</PRE>';
  3. Add a standard application in the Partner account to get client_id and client_secret and when saving the app.
  4. Indicate the parameter values client_id and client_secret inside the file settings.php for OAuth 2.0 authorization in define C_REST_CLIENT_ID and C_REST_CLIENT_SECRET.
    <?
    require_once('crest.php');
    
    // put an example below
    echo '<PRE>';
    print_r(CRest::call(
       'crm.lead.add',
       [
          'fields' =>[
          'TITLE' => 'Lead Name',//Title*[string]
          'NAME' => 'Name',//Name[string]
          'LAST_NAME' => 'Last name',/?Last name[string]
          ]
       ])
    );
    
    echo '</PRE>';
  5. Add version and your app URL (https://mydomain.xxx/index.php) and installation script URL in the app details (https://mydomain.xxx/install.php) in the version details.
  6. After saving, open the version details and by clicking on the link "Install on my Bitrix24", install your app to any your Bitrix24 account available to you.
  7. After installation is complete, you will see results of the example (If the example demonstrates inbuilt widgets into other Bitrix24 tools, you need to go into these tools).
  8. For a finished mass-distributed application, CRest class must be inherited by re-defining the methods getSettingData/setSettingData, which get/save authorization tokens int


© «Bitrix24», 2001-2024
Up