Last active
July 4, 2016 14:05
-
-
Save BulatSa/f5630ec532690948bac251a39d0c2032 to your computer and use it in GitHub Desktop.
Unisender Add User To List
This file contains hidden or 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
// Unisender | |
require_once("unisender_api.php"); //подключаем файл класса | |
$api_key = "123456"; //API-ключ к вашему кабинету | |
$uni = new UniSenderApi($api_key); //создаем экземляр класса, с которым потом будем работать | |
// Данные о новом подписчике | |
$user_lists = "7114782"; | |
$user_tag = urlencode("Added using API"); | |
if (!$user_phone) { | |
$user_phone = ""; | |
} | |
// Создаём POST-запрос | |
$POST = array ( | |
'api_key' => $api_key, | |
'list_ids' => $user_lists, | |
'fields[email]' => $user_email, | |
'fields[Name]' => $user_name, | |
'fields[phone]' => $user_phone, | |
//'request_ip' => $user_ip, | |
'double_optin' => 3, | |
'tags' => $user_tag | |
); | |
// Устанавливаем соединение | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_URL, 'http://api.unisender.com/ru/api/subscribe?format=json'); | |
$result = curl_exec($ch); | |
if ($result) { | |
// Раскодируем ответ API-сервера | |
$jsonObj = json_decode($result); | |
if(null===$jsonObj) { | |
// Ошибка в полученном ответе | |
echo "Invalid JSON"; | |
} | |
elseif(!empty($jsonObj->error)) { | |
// Ошибка добавления пользователя | |
echo "An error occured: " . $jsonObj->error . "(code: " . $jsonObj->code . ")"; | |
} else { | |
// Новый пользователь успешно добавлен | |
echo "Added. ID is " . $jsonObj->result->person_id; | |
} | |
} else { | |
// Ошибка соединения с API-сервером | |
echo "API access error"; | |
} | |
// End Unisender |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment