-
-
Save LipinArts/4d8c55a17e9202ea2937b252e7462ebc to your computer and use it in GitHub Desktop.
Добавление контакта и сделки в amocrm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
//amo | |
//ПРЕДОПРЕДЕЛЯЕМЫЕ ПЕРЕМЕННЫЕ | |
$responsible_user_id = 7292136; //id ответственного по сделке, контакту, компании | |
$lead_name = 'Заявка с сайта'; //Название добавляемой сделки | |
$lead_status_id = '11331793'; //id этапа продаж, куда помещать сделку | |
$contact_name = $cname; //Название добавляемого контакта | |
$contact_phone = $cphone; //Телефон контакта | |
$contact_email = $cemail; //Емейл контакта | |
//АВТОРИЗАЦИЯ | |
$user=array( | |
'USER_LOGIN'=>'ceo@penbrain.ru', #Ваш логин (электронная почта) | |
'USER_HASH'=>'aac0ab625826074d756b1b1344cc2bc9' #Хэш для доступа к API (смотрите в профиле пользователя) | |
); | |
$subdomain='penbrain'; | |
#Формируем ссылку для запроса | |
$link='https://'.$subdomain.'.amocrm.ru/private/api/auth.php?type=json'; | |
$curl=curl_init(); #Сохраняем дескриптор сеанса cURL | |
#Устанавливаем необходимые опции для сеанса cURL | |
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_POST,true); | |
curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($user)); | |
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-код ответа сервера | |
curl_close($curl); #Завершаем сеанс cURL | |
$Response=json_decode($out,true); | |
//echo '<b>Авторизация:</b>'; echo '<pre>'; print_r($Response); echo '</pre>'; | |
//ПОЛУЧАЕМ ДАННЫЕ АККАУНТА | |
$link='https://'.$subdomain.'.amocrm.ru/private/api/v2/json/accounts/current'; #$subdomain уже объявляли выше | |
$curl=curl_init(); #Сохраняем дескриптор сеанса cURL | |
#Устанавливаем необходимые опции для сеанса cURL | |
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); | |
curl_close($curl); | |
$Response=json_decode($out,true); | |
$account=$Response['response']['account']; | |
//echo '<b>Данные аккаунта:</b>'; echo '<pre>'; print_r($Response); echo '</pre>'; | |
//ПОЛУЧАЕМ СУЩЕСТВУЮЩИЕ ПОЛЯ | |
$amoAllFields = $account['custom_fields']; //Все поля | |
$amoConactsFields = $account['custom_fields']['contacts']; //Поля контактов | |
//echo '<b>Поля из амо:</b>'; echo '<pre>'; print_r($amoConactsFields); echo '</pre>'; | |
//ФОРМИРУЕМ МАССИВ С ЗАПОЛНЕННЫМИ ПОЛЯМИ КОНТАКТА | |
//Стандартные поля амо: | |
$sFields = array_flip(array( | |
'PHONE', //Телефон. Варианты: WORK, WORKDD, MOB, FAX, HOME, OTHER | |
'EMAIL' //Email. Варианты: WORK, PRIV, OTHER | |
) | |
); | |
//Проставляем id этих полей из базы амо | |
foreach($amoConactsFields as $afield) { | |
if(isset($sFields[$afield['code']])) { | |
$sFields[$afield['code']] = $afield['id']; | |
} | |
} | |
//ДОБАВЛЯЕМ СДЕЛКУ | |
$leads['request']['leads']['add']=array( | |
array( | |
'name' => $lead_name, | |
'status_id' => $lead_status_id, //id статуса | |
'responsible_user_id' => $responsible_user_id, //id ответственного по сделке | |
//'date_create'=>1298904164, //optional | |
//'price'=>300000, | |
//'tags' => 'Important, USA', #Теги | |
//'custom_fields'=>array() | |
) | |
); | |
$link='https://'.$subdomain.'.amocrm.ru/private/api/v2/json/leads/set'; | |
$curl=curl_init(); #Сохраняем дескриптор сеанса cURL | |
#Устанавливаем необходимые опции для сеанса cURL | |
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_CUSTOMREQUEST,'POST'); | |
curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($leads)); | |
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json')); | |
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); | |
$Response=json_decode($out,true); | |
//echo '<b>Новая сделка:</b>'; echo '<pre>'; print_r($Response); echo '</pre>'; | |
if(is_array($Response['response']['leads']['add'])) | |
foreach($Response['response']['leads']['add'] as $lead) { | |
$lead_id = $lead["id"]; //id новой сделки | |
}; | |
//ДОБАВЛЯЕМ СДЕЛКУ - КОНЕЦ | |
//ДОБАВЛЕНИЕ КОНТАКТА | |
$contact = array( | |
'name' => $contact_name, | |
'linked_leads_id' => array($lead_id), //id сделки | |
'responsible_user_id' => $responsible_user_id, //id ответственного | |
'custom_fields'=>array( | |
array( | |
'id' => $sFields['PHONE'], | |
'values' => array( | |
array( | |
'value' => $contact_phone, | |
'enum' => 'MOB' | |
) | |
) | |
), | |
array( | |
'id' => $sFields['EMAIL'], | |
'values' => array( | |
array( | |
'value' => $contact_email, | |
'enum' => 'WORK' | |
) | |
) | |
) | |
) | |
); | |
$set['request']['contacts']['add'][]=$contact; | |
#Формируем ссылку для запроса | |
$link='https://'.$subdomain.'.amocrm.ru/private/api/v2/json/contacts/set'; | |
$curl=curl_init(); #Сохраняем дескриптор сеанса cURL | |
#Устанавливаем необходимые опции для сеанса cURL | |
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_CUSTOMREQUEST,'POST'); | |
curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($set)); | |
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json')); | |
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); | |
CheckCurlResponse($code); | |
$Response=json_decode($out,true); | |
//ДОБАВЛЕНИЕ КОНТАКТА - КОНЕЦ | |
//amo | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Можешь подсказать, как сюда добавить переменную "город"?