Skip to content

Instantly share code, notes, and snippets.

@goastoman
Created January 27, 2018 09:06
Show Gist options
  • Select an option

  • Save goastoman/15ed3cd74b27cbb2448bb7ef3e69d9b5 to your computer and use it in GitHub Desktop.

Select an option

Save goastoman/15ed3cd74b27cbb2448bb7ef3e69d9b5 to your computer and use it in GitHub Desktop.

Revisions

  1. goastoman created this gist Jan 27, 2018.
    86 changes: 86 additions & 0 deletions list.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    $subdomain = 'ХХХ.com'; #Наш аккаунт - поддомен


    /*
    Подключаемся
    */

    $link='https://'.$subdomain.'.amocrm.ru/private/api/v2/json/leads/list';

    $curl=curl_init();

    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_USERAGENT,'amoCRM-API-client/1.0');
    curl_setopt($curl,CURLOPT_URL,$link);
    curl_setopt($curl,CURLOPT_HEADER,false);
    curl_setopt($curl,CURLOPT_COOKIEFILE,dirname(__FILE__).'/cookie.txt'); #PHP>5.3.6 dirname(__FILE__) -> __DIR__
    curl_setopt($curl,CURLOPT_COOKIEJAR,dirname(__FILE__).'/cookie.txt'); #PHP>5.3.6 dirname(__FILE__) -> __DIR__
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);

    $out=curl_exec($curl); #Инициируем запрос к API и сохраняем ответ в переменную
    $code=curl_getinfo($curl,CURLINFO_HTTP_CODE); #Получим HTTP-код ответа сервера

    $code=(int)$code;
    $errors=array(
    301=>'Moved permanently',
    400=>'Bad request',
    401=>'Unauthorized',
    403=>'Forbidden',
    404=>'Not found',
    500=>'Internal server error',
    502=>'Bad gateway',
    503=>'Service unavailable'
    );
    try
    {
    if($code!=200 && $code!=204) {
    throw new Exception(isset($errors[$code]) ? $errors[$code] : 'Undescribed error',$code);
    }
    }
    catch(Exception $E)
    {
    die('Ошибка: '.$E->getMessage().PHP_EOL.'Код ошибки: '.$E->getCode());
    }


    /*
    Формат JSON -> Формат PHP
    */

    $Response=json_decode($out,true);
    $Response=$Response['_embedded']['items'];


    /*
    Находим сделки без задач и добавляем
    */

    $leads_id = array();

    foreach($leads['closest_task'] == 0) {
    array_push($leads_id, $leads['id']);
    }

    $tasks['request']['tasks']['add'] = array();
    foreach($leads_id as $id) {
    array_push($tasks['request']['tasks']['add'], array(
    'element_id' => $id, #ID
    'element_type' => 2, #1 - контакт, 2- сделка, 3 - компания, 12 - покупатель
    'task_type' => 1, #1 - звонок, 2 - Встреча, 3 - Написать письмо
    'text' => 'Сделка без задачи'
    )
    );
    }

    /*
    Отправляем запрос на обновление задач
    */

    $output='ID добавленных задач:'.PHP_EOL;
    foreach($Response as $v)
    if(is_array($v))
    $output.=$v['id'].PHP_EOL;
    echo $output;

    curl_close($curl); #Завершаем сеанс cURL