187 lines
6.8 KiB
PHP
187 lines
6.8 KiB
PHP
|
|
<?php error_reporting(E_ALL);
|
|||
|
|
ini_set('display_errors', 1);
|
|||
|
|
/* SigmaSMS REST API
|
|||
|
|
* https://online.sigmasms.ru/docs/#/api/HTTP-REST
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
// Универсальная функция отправки
|
|||
|
|
function apiRequest($first = false, $data = false, $url_path = false, $token = false, $file = false) {
|
|||
|
|
global $token_filename;
|
|||
|
|
$api_url = 'https://online.sigmasms.ru/api/';
|
|||
|
|
$login = 'kfv.advokat@gmail.com';
|
|||
|
|
$pass = 's7NRIb';
|
|||
|
|
// Get Token
|
|||
|
|
if ($first) {
|
|||
|
|
$fields = array(
|
|||
|
|
'username' => $login,
|
|||
|
|
'password' => $pass,
|
|||
|
|
'type' => 'local'
|
|||
|
|
);
|
|||
|
|
$ch = curl_init();
|
|||
|
|
curl_setopt($ch, CURLOPT_URL, $api_url.'login');
|
|||
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=UTF-8"));
|
|||
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
|
|||
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|||
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|||
|
|
$response = curl_exec($ch);
|
|||
|
|
curl_close($ch);
|
|||
|
|
if (!$response) {
|
|||
|
|
$response = json_encode(array('error' => 'true'));
|
|||
|
|
} else {
|
|||
|
|
file_put_contents($token_filename, $response);
|
|||
|
|
}
|
|||
|
|
} elseif ($url_path && $token) {
|
|||
|
|
$ch = curl_init();
|
|||
|
|
curl_setopt($ch, CURLOPT_URL, $api_url.$url_path);
|
|||
|
|
if ($file) {
|
|||
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: ".mime_content_type($data['file']), "Content-length: ".filesize($data['file']), "Authorization: ".$token));
|
|||
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($data['file']));
|
|||
|
|
} else {
|
|||
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json;charset=UTF-8", "Accept: application/json", "Authorization: ".$token));
|
|||
|
|
if ($data && is_array($data)) {
|
|||
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $data ));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|||
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|||
|
|
$response = curl_exec($ch);
|
|||
|
|
curl_close($ch);
|
|||
|
|
if (!$response) {
|
|||
|
|
$response = json_encode(array('error' => 'true'));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
header("Content-Type: application/json;charset=UTF-8");
|
|||
|
|
return $response;
|
|||
|
|
}
|
|||
|
|
// Авторизация и получение токена
|
|||
|
|
function apiAuth() {
|
|||
|
|
// Название файла для сохранения токена
|
|||
|
|
$token_filename = 'sigmatoken.txt';
|
|||
|
|
// проверяем токен
|
|||
|
|
if (file_exists($token_filename) && (date('Y-m-d H:i:s', filemtime($token_filename)) > date('Y-m-d H:i:s', strtotime('-23 hours')))) {
|
|||
|
|
$result = file_get_contents($token_filename, true);
|
|||
|
|
} else {
|
|||
|
|
$result = apiRequest(true);
|
|||
|
|
}
|
|||
|
|
//
|
|||
|
|
$unjes = json_decode($result);
|
|||
|
|
if (isset($unjes->token) && !empty($unjes->token)) {
|
|||
|
|
$token = (string) $unjes->token;
|
|||
|
|
} else {
|
|||
|
|
$token = null;
|
|||
|
|
}
|
|||
|
|
return $token;
|
|||
|
|
}
|
|||
|
|
//
|
|||
|
|
function clear_phone($phone) {
|
|||
|
|
$phone_number = preg_replace('/[() -]+/', '', $phone);
|
|||
|
|
return $phone_number;
|
|||
|
|
}
|
|||
|
|
// Загрузка файла
|
|||
|
|
function uploadFile($file_path) {
|
|||
|
|
$token = apiAuth();
|
|||
|
|
if ($token) {
|
|||
|
|
$dataFile = array('file' => dirname(__FILE__).'/'.$file_path);
|
|||
|
|
return apiRequest(false, $dataFile, 'storage', $token, true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// Отправка одиночного сообщения
|
|||
|
|
function sendOneMess($type, $recipient, $sender, $text, $button = null, $image = null) {
|
|||
|
|
$token = apiAuth();
|
|||
|
|
$token = 'bbdbe4516d8935e1169a1b7a7c0914dcabcad53dd6bee56bcc77974d4334b650';
|
|||
|
|
if ($token) {
|
|||
|
|
$params = array(
|
|||
|
|
"type" => $type,
|
|||
|
|
"recipient" => clear_phone($recipient),
|
|||
|
|
"payload" => array(
|
|||
|
|
"sender" => $sender,
|
|||
|
|
"text" => $text,
|
|||
|
|
"button" => $button,
|
|||
|
|
"image" => $image
|
|||
|
|
)
|
|||
|
|
);
|
|||
|
|
return apiRequest(false, $params, 'sendings', $token);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// Отправка каскада
|
|||
|
|
function sendCascade($data) {
|
|||
|
|
$token = apiAuth();
|
|||
|
|
if ($token) {
|
|||
|
|
return apiRequest(false, $data, 'sendings', $token);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// Проверка статуса
|
|||
|
|
function checkStatus($id) {
|
|||
|
|
if ($id) {
|
|||
|
|
$token = apiAuth();
|
|||
|
|
if ($token) {
|
|||
|
|
return apiRequest(false, false, 'sendings/'.$id, $token);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* Тесты */
|
|||
|
|
$myphone = $_POST['phonenumber'];
|
|||
|
|
echo 'Тест СМС: '.PHP_EOL;
|
|||
|
|
$sendSms = sendOneMess('sms', $myphone, 'lexpriority', 'Код подтверждения: '.$_POST['smscode']);
|
|||
|
|
var_dump($sendSms);
|
|||
|
|
|
|||
|
|
// echo PHP_EOL.'Проверка статуса сообщения: '.PHP_EOL;
|
|||
|
|
// var_dump(checkStatus('6035fe28-2f60-4973-8681-jhjh887990087'));
|
|||
|
|
|
|||
|
|
// echo PHP_EOL.'Загрузка картинки: '.PHP_EOL;
|
|||
|
|
// $upload_image = uploadFile('test.png');
|
|||
|
|
// var_dump($upload_image);
|
|||
|
|
// echo 'Проверить корректность загрузки можно по ссылке: https://online.sigmasms.ru/api/storage/{user_id}/{image_key}'.PHP_EOL;
|
|||
|
|
|
|||
|
|
// echo PHP_EOL.'Тест Viber: '.PHP_EOL;
|
|||
|
|
// $msg_image = json_decode($upload_image);
|
|||
|
|
// if (isset($msg_image->key)) {
|
|||
|
|
// var_dump(sendOneMess('viber', $myphone, 'X-City', 'Тест сообщения Viber', array('text' => 'Текст кнопки', 'url' => 'https://google.ru'), $msg_image->key));
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// echo PHP_EOL.'Каскадная переотправка VK->Viber->SMS: '.PHP_EOL;
|
|||
|
|
// $cascadeData = array(
|
|||
|
|
// "type" => 'vk',
|
|||
|
|
// "recipient" => clear_phone($myphone),
|
|||
|
|
// "payload" => array(
|
|||
|
|
// "sender" => 'sigmasmsru',
|
|||
|
|
// "text" => 'Тест сообщения ВК',
|
|||
|
|
// ),
|
|||
|
|
// "fallbacks" => [
|
|||
|
|
// array(
|
|||
|
|
// "type" => 'viber',
|
|||
|
|
// "payload" => array(
|
|||
|
|
// "sender" => 'X-City',
|
|||
|
|
// "text" => 'Тест сообщения Viber',
|
|||
|
|
// "image" => $msg_image->key,
|
|||
|
|
// "button" => array(
|
|||
|
|
// "text" => "Текст кнопки",
|
|||
|
|
// "url" => 'https://google.ru',
|
|||
|
|
// ),
|
|||
|
|
// ),
|
|||
|
|
// '$options' => array(
|
|||
|
|
// "onStatus" => ["failed"],
|
|||
|
|
// "onTimeout" => array(
|
|||
|
|
// "timeout" => 120,
|
|||
|
|
// "except" => ["delivered", "seen"]
|
|||
|
|
// )
|
|||
|
|
// )
|
|||
|
|
// ),
|
|||
|
|
// array(
|
|||
|
|
// "type" => "sms",
|
|||
|
|
// "payload" => array(
|
|||
|
|
// "sender" => "SigmaSMS",
|
|||
|
|
// "text" => 'Тест сообщения СМС'
|
|||
|
|
// ),
|
|||
|
|
// '$options' => array(
|
|||
|
|
// "onStatus" => ["failed"],
|
|||
|
|
// "onTimeout" => array(
|
|||
|
|
// "timeout" => 120,
|
|||
|
|
// "except" => ["delivered", "seen"]
|
|||
|
|
// )
|
|||
|
|
// )
|
|||
|
|
// )
|
|||
|
|
// ]
|
|||
|
|
// );
|
|||
|
|
// var_dump(sendCascade($cascadeData));
|