<?php
$subdomain = 'test';
$offset = 0;
$size = 500;
$link = "https://$subdomain.amocrm.ru/api/v2/leads";
$temp = [];
do {
$res = request($link, 'USER_LOGIN', 'USER_HASH', [
'limit_rows' => $size,
'limit_offset' => $offset
]);
$offset += $size;
if ($itsOk = ($res['code'] === 200)) {
$temp[] = $res['response']['_embedded']['items'];
}
} while ($itsOk);
$leads = array_merge([], ...$temp);
$leadsIds = array_column($leads, 'id');
var_dump($leadsIds);
/* Для API-Key */
function request($link, $user_login, $user_hash, $params = [])
{
$params['USER_LOGIN'] = $user_login;
$params['USER_HASH'] = $user_hash;
$tries = 0;
do {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $link . '?' . http_build_query($params));
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$out = curl_exec($curl);
$code = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code === 429) {
if ($tries++ > 3) {
throw new RuntimeException('Превышено допустимое количество запросов в секунду');
}
sleep(1);
}
} while ($code === 429);
return ['code' => $code, 'response' => json_decode($out, 1)];
}
/* Для OAuth
function request($link, $access_token, $params = [])
{
$tries = 0;
do {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $link . '?' . http_build_query($params));
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token));
$out = curl_exec($curl);
$code = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code === 429) {
if ($tries++ > 3) {
throw new RuntimeException('Превышено допустимое количество запросов в секунду');
}
sleep(1);
}
} while ($code !== 429);
return ['code' => $code, 'response' => json_decode($out, 1)];
}
*/