<?php
require($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/include/prolog_before.php');
// Получаем список email всех активных юзеров
global $USER;
$filter = Array(
"ACTIVE" => "Y",
);
$email = Array();
$rsUsers = CUser::GetList(($by = "NAME"), ($order = "desc"), $filter);
while ($arUser = $rsUsers->Fetch()) {
$email[] = $arUser['EMAIL'];
}
// Массив с исключениями
$array = Array(
"retailcrm.com",
"retailcrm.ru",
"sberbank.ru",
"matveevs.ru"
);
/**
* Ищем если совпадение
* @param string $value Искомое значение
* @param Array $array Массив исключений
* @return Boolean Результат
*/
function strposArray($value, $array){
$res = false;
foreach ($array as $key) {
$res = strpos($value, $key) ? true : $res;
}
return $res;
}
// удаляем с исключением
foreach ($email as $key => $value){
if (strposArray($value, $array)){
unset($email[$key]);
}
}
/**
* @param NULL
* @return mixed
*/
function getToken($client_id, $client_secret)
{
$param = array(
'grant_type' => 'client_credentials',
'client_id' => $client_id,
'client_secret' => $client_secret,
);
$paramSend = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($param),
),
);
$result = file_get_contents("https://api.sendpulse.com/oauth/access_token", false, stream_context_create($paramSend));
$json = json_decode($result);
return $json->access_token;
}
/**
* @param $token mixed
* @return email[]
*/
function addEmails($token, $array, $book_id)
{
$param = array(
'emails' => serialize(createMassive(getEmails($array))),
);
$paramSend = array(
'http' => array(
'method' => 'POST',
'header' => 'Authorization: Bearer ' . $token,
'content' => http_build_query($param),
),
);
$result = file_get_contents("https://api.sendpulse.com/addressbooks/" . $book_id . "/emails", false, stream_context_create($paramSend));
$json = json_decode($result);
return $json->result;
}
function getEmails($array)
{
return $array;
}
/**
* @param $array emails
* @return array
*/
function createMassive($array)
{
$result = array();
foreach ($array as $key) {
array_push($result, $key);
}
return $result;
}
$token = getToken("cliend_id", "client_secret");
$result = addEmails($token, $email, 'book_id');
echo $result ? "success" : "error";