I
I
Ilya Tsarenkov2021-10-26 20:37:45
API
Ilya Tsarenkov, 2021-10-26 20:37:45

How to download all companies from bitrks24 via api, having crest, with a request via batch, if there are more than 2500 of them?

It so happened that I once had to download a complete list of companies using the Bitrix API. In the beginning, for a long time I could not figure out what and how, from the fact that, somehow, it seemed to me that the information was meager.
However, then I figured it out and decided to file this question. Maybe someone will come in handy, because often I'm on this resource and I'm watching something myself.
And so, after smoking this manual https://dev.1c-bitrix.ru/learning/course/index.ph...
how to install CREST so that you can batch communicate with bitrix24 using batch.
The following has been done
1) Files from the archive https://dev.1c-bitrix.ru/docs/marketplace-and-apps... have been
installed to the north 2) A local application has been registered using only the API in Bitrix
3) The REST_CLIENT_ID and CLIENT_SECRET codes received in Bitrix are entered into the settings.php file on the server

After successful registration, the question arose of how 'crm.company.list' works.
Since its call by the call method produces only the first 50 results.
The official documentation does not say. how exactly to display the next 50 maybe I didn’t look well, of course, but in the end, after a long search, it was found that it’s enough to add such a request to the method? start = 50 for a page request.
Well, in order to call a lot at once with one request, you just need to combine such requests into a batch request, however, it has a limitation when it reaches 50 such a request, you will have to make the next batch request and start from where the previous one left off.

<?php
require_once (__DIR__.'/crest.php');
$result = CRest::call('batch',
            array(
                'halt' => 0,
                'cmd'=> array(
                'list_0' => 'crm.company.list',
                'list_1' =>'crm.company.list?start=50',
                'list_2' =>'crm.company.list?start=100',
                      )
               )
          );
print_r ($result);
?>

I presented a primitive implementation of the request, maybe someone will throw off its universal version, where the result will be a full array of companies? Or maybe there is already a written version not on the knee, share it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Tsarenkov, 2021-10-26
@ILX

Continuing the topic...
A universal function that will return all companies, no matter how many there are, don't throw tomatoes at shitcode...

function call_all_company ()//функция которая вернет все комапнии из bitrix24
    {
      $total = CRest::call('crm.company.list')["total"];
    
      $list_N=(int)($total/50)+1; //Количество необходимых листов +1 тк от нуля
      for($i=0; $i < $list_N; $i++)
      {
        $cmd_arr_cach[(int)($i/49)]["list_".$i] = 'crm.company.list?start='.($i*50);
      }
      foreach ($cmd_arr_cach as $key => $cmd_arr)
      {
        sleep(1);//Щадяший режим лучше ставить 2 секунды
        
        $local = CRest::call('batch',
                    array(
                        'halt' => 0,
                        'cmd'=> $cmd_arr
                      )
                    );
                    
        $result[]= call_user_func_array('array_merge', $local['result']['result']);
      }
      return $result[0];
    }
     print_r (call_all_company ());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question