<?php
class AmoApi {
private $params = array('USER_LOGIN' =>'', 'USER_HASH' => '');
private $link = '';
const MAIN_LINK = '';
private $post_params;
/**
* @param mixed $post_params
*/
public function setPostParams($post_params) {
if($post_params['placement'] == 1) {
$post_params['placement'] = '2040058';//Да
}elseif($post_params['placement'] == 0){
$post_params['placement'] = '2040060';//Нет
}
$this->post_params = $post_params;
}
public function __construct(){
$this->auth();
}
/**
* @param mixed $link
*/
public function setLink($link) {
$this->link = $link;
}
public function getCurrentUserInfo(){
$arr = $this->curlGet();
return $arr['response']['account'];
}
protected function auth() {
return $this->curlPost($this->params, self::MAIN_LINK.$this->link);
}
protected function curlPost($params, $link) {
$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($params));
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); #Получим HTTP-код ответа сервера
curl_close($curl); #Заверашем сеанс cURL
return json_decode($out, true);
}
protected function curlGet($link = '') {
$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);
/**
* Данные получаем в формате JSON, поэтому, для получения читаемых данных,
* нам придётся перевести ответ в формат, понятный PHP
*/
$Response=json_decode($out,true);
return $Response;
}
protected function addLeads() {
$link = self::MAIN_LINK.'private/api/v2/json/leads/set';
$current_user = $this->getCurrentUserInfo();
$params['request']['leads']['add']= array(
array(
'name'=>'Заявка с сайта № ' .$this->getNumRequest(),
'status_id'=>7540914, //Необработанная заявка
// 'price'=>5000,
'responsible_user_id'=>209582,
'custom_fields'=>array(
//Источник заявки
array(
'id' => 905986,
'values' =>array(
array('value'=>"2050856")//2050856 => string 'Сайт'
)
),
//Трудоустроен
array(
'id' => 901454,
'values' =>array(
array('value'=>$this->post_params['placement'])
)
),
//Сумма кредита
array(
'id' => 896588,
'values' => array(array('value'=>$this->post_params['summa']))
)
)
)
);
return $this->curlPost($params, $link)['response']['leads']['add'];
}
protected function addContacts($lead_id = 0)
{
$link = self::MAIN_LINK . 'private/api/v2/json/contacts/set';
$params['request']['contacts']['add'] = array(
array(
'name' => $this->post_params['name'],//$contact['name'],
'linked_leads_id' => array(
$lead_id
),
'custom_fields' => array(
array(
//Телефоны
'id' => 861010, //Уникальный индентификатор заполняемого дополнительного поля
'values' => array(
array(
'value' => $this->post_params['phone'],//$contact_phone,
'enum' => 'WORK' //Мобильный
)
)
),
array(
//E-mails
'id' => 861012,
'values' => array(
array(
'value' => $this->post_params['email'],//$contact_email,
'enum' => 'WORK', //Рабочий
)
)
),
array(
//Город проживания
'id' => 901490,
'values' => array(
array(
'value' => $this->post_params['region']
)
)
),
//Полных лет
array(
'id' => 896596,
'values' => array(array('value'=>$this->post_params['age']))
)
)
)
);
return $this->curlPost($params, $link);
}
public function addConnectedRow() {
$lead_id = $this->addLeads()[0]['id'];
$this->error[] = $lead_id;
$contact = $this->addContacts($lead_id);
$this->error[] = $contact;
return $contact;
}
protected function getNumRequest(){
$file = 'request_num';
if(file_exists($file)){
$id = (int)file_get_contents($file) + 1;
file_put_contents($file, $id);
return $id;
}
file_put_contents($file, 1);
return 1;
}
}
preferences:
36.98 ms | 402 KiB | 5 Q