Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@progreccor
Created September 10, 2019 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save progreccor/59a8368da47b932df3c1a6b00a3ea093 to your computer and use it in GitHub Desktop.
Save progreccor/59a8368da47b932df3c1a6b00a3ea093 to your computer and use it in GitHub Desktop.
<?php
defined('_JEXEC') or die;
#Массив с параметрами, которые нужно передать методом POST к API системы
$login="test@yandex.ru";
$api_key='2672fefef7b71620b86d1a8b90f6ab1e' ; #ключ фейковый - замените на свой
$subdomain='test2'; #Наш аккаунт - поддомен
$contact_name = 'Имя не указано';
if(isset($source['name']))
{
$contact_name=$source['name'];
}
else
{
if(isset($source['mane']))
{
$contact_name=$source['mane'];
}
}
$date = date("d-m-Y");
$datetime = strtotime($date);
//ПРЕДОПРЕДЕЛЯЕМЫЕ ПЕРЕМЕННЫЕ
$responsible_user_id = 2179495; //id ответственного по сделке, контакту, компании
$contact_phone = $source['phone']; //Телефон контакта
$pagetitle='';
if(isset($source['pagetitle']))
{
$pagetitle=$source['pagetitle'];
}
$reffer='';
if(isset($source['reffer']))
{
$reffer=$source['reffer'];
}
$lid='';
if(isset($source['lid']))
{
$lid=$source['lid'];
}
$tags="TRUCK, ".$lid;
$email='';
if(isset($source['email']))
{
$email=$source['email'];
}
$mark_and_model='';
if(isset($source['mark_and_model']))
{
$mark_and_model=$source['mark_and_model'];
}
$vin='';
if(isset($source['vin']))
{
$vin=$source['vin'];
}
$lid='';
if(isset($source['lid']))
{
$lid=$source['lid'];
}
$mark='';
if(isset($source['mark']))
{
$mark=$source['mark'];
}
$model='';
if(isset($source['model']))
{
$model=$source['model'];
}
$year='';
if(isset($source['year']))
{
$year=$source['year'];
}
$volume='';
if(isset($source['volume']))
{
$volume=$source['volume'];
}
$power='';
if(isset($source['power']))
{
$power=$source['power'];
}
$fuel='';
if(isset($source['fuel']))
{
$fuel=$source['fuel'];
}
$transmission='';
if(isset($source['transmission']))
{
$transmission=$source['transmission'];
}
$marking='';
if(isset($source['marking']))
{
$marking=$source['marking'];
}
$equipment='';
if(isset($source['equipment']))
{
$equipment=$source['equipment'];
}
$url='';
if(isset($source['url']))
{
$url=$source['url'];
}
/* Для начала нам необходимо инициализировать данные, необходимые для составления запроса. */
$data = array (
'add' =>
array (
0 =>
array (
'source_name' => $pagetitle,
'created_at' => $datetime,
'pipeline_id' => "1017340",
'incoming_entities' =>
array (
'contacts' =>
array (
0 =>
array (
'name' => $contact_name,
'custom_fields' =>
array (
array(
'id' => '348817',
'values' => array(
array(
'value' => $contact_phone,
'enum' => 'MOB'
)
)
),
array(
'id' => '348819',
'values' => array(
array(
'value' => $email,
'enum' => 'WORK'
)
)
)
),
),
),
'leads' =>
array (
0 =>
array (
'name' => $lid,
'responsible_user_id' => '504141',
'tags' => $tags,
'custom_fields' =>
array (
//VIN:
array(
'id' => "473807",
'values' => array(
array(
'value' => $vin,
)
)
),
//e-mail:
array(
'id' => "558305",
'values' => array(
array(
'value' => $email,
)
)
),
//Марка:
array(
'id' => "473799",
'values' => array(
array(
'value' => $mark_and_model,
)
)
),
//Модель авто:
array(
'id' => "473799",
'values' => array(
array(
'value' => $model,
)
)
),
//Выпуск:
array(
'id' => "473809",
'values' => array(
array(
'value' => $year,
)
)
),
//Объём:
array(
'id' => "473803",
'values' => array(
array(
'value' => $volume,
)
)
),
//Мощность:
array(
'id' => "473811",
'values' => array(
array(
'value' => $power,
)
)
),
//Двигатель:
array(
'id' => "473801",
'values' => array(
array(
'value' => $fuel,
)
)
),
//Коробка передач:
array(
'id' => "493257",
'values' => array(
array(
'value' => $transmission,
)
)
),
//Коробка передач:
array(
'id' => "508673",
'values' => array(
array(
'value' => $mark,
)
)
),
array(
'id' => "508843",
'values' => array(
array(
'value' => $equipment,
)
)
),
array(
'id' => "558305",
'values' => array(
array(
'value' => $email,
)
)
),
array(
'id' => "473795",
'values' => array(
array(
'value' => $url,
)
)
),
//Маркировка:
array(
'id' => "493271",
'values' => array(
array(
'value' => $marking,
)
)
)
),
),
),
),
'incoming_lead_info' =>
array (
'form_id' => "1",
'form_page' => $url,
'ip' => '127.0.0.1',
'form_name' => $lid,
'from' => 'TRUCK',
'referer' => $reffer,
),
),
),
);
#Формируем ссылку для запроса
$link='https://'.$subdomain.'.amocrm.ru/api/v2/incoming_leads/form?api_key='.$api_key.'&login='.$login;
$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_HTTPHEADER,['Accept: application/json']);
curl_setopt($curl,CURLOPT_URL,$link);
curl_setopt($curl,CURLOPT_HEADER,false);
curl_setopt($curl,CURLOPT_POSTFIELDS, http_build_query($data));
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);
/* Теперь мы можем обработать ответ, полученный от сервера. Это пример. Вы можете обработать данные своим способом. */
$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'
);
#Если код ответа не равен 200, 201 или 204 - возвращаем сообщение об ошибке
if(!in_array($code, [200, 201, 204]))
{
$mess = (isset($errors[$code]) ? $errors[$code] : 'Undescribed error '.$code);
$in=[];
$in["message"]=$mess;
JLog::add(json_encode($in), JLog::WARNING, 'plg_system_radicalform');
}
//$Response=json_decode($out,true);
//$Response=$Response['data'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment