system/library/sms/sms_gateway.php
Вот содержимое файла system/library/sms/sms_gateway.php
:
<?php
// SMS Gateway Library for Task Manager - Handles SMS sending via various SMS gateways
class SMSGateway {
private $api_url;
private $api_key;
private $sender_id;
public function __construct($api_url, $api_key, $sender_id) {
$this->api_url = $api_url;
$this->api_key = $api_key;
$this->sender_id = $sender_id;
}
// Send SMS using the configured SMS gateway
public function sendSMS($phone_number, $message) {
// Prepare the payload for the SMS request
$data = array(
'api_key' => $this->api_key,
'sender_id' => $this->sender_id,
'to' => $phone_number,
'message' => $message
);
// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Execute the cURL request and get the response
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
$error_message = curl_error($ch);
curl_close($ch);
return array('success' => false, 'error' => $error_message);
} else {
curl_close($ch);
return array('success' => true, 'response' => $response);
}
}
// Send SMS via SMS.ru
public function sendSMSRU($phone_number, $message) {
// Example for SMS.ru (adjust parameters accordingly)
$api_url = 'https://sms.ru/sms/send';
$api_key = 'your_smsru_api_key';
$params = array(
'api_id' => $api_key,
'to' => $phone_number,
'msg' => $message,
'json' => 1
);
return $this->sendRequest($api_url, $params);
}
// Send SMS via Twilio
public function sendTwilio($phone_number, $message) {
// Example for Twilio API (adjust with your Twilio credentials)
$account_sid = 'your_twilio_account_sid';
$auth_token = 'your_twilio_auth_token';
$from_number = 'your_twilio_phone_number';
$url = "https://api.twilio.com/2010-04-01/Accounts/$account_sid/Messages.json";
$data = array(
'To' => $phone_number,
'From' => $from_number,
'Body' => $message
);
// Make the HTTP request to Twilio
return $this->sendRequest($url, $data, $account_sid, $auth_token);
}
// Send SMS via Plivo
public function sendPlivo($phone_number, $message) {
// Example for Plivo API (adjust with your Plivo credentials)
$auth_id = 'your_plivo_auth_id';
$auth_token = 'your_plivo_auth_token';
$src_number = 'your_plivo_phone_number';
$url = "https://api.plivo.com/v1/Account/$auth_id/Message/";
$data = array(
'src' => $src_number,
'dst' => $phone_number,
'text' => $message
);
return $this->sendRequest($url, $data, $auth_id, $auth_token);
}
// Send SMS via Vonage
public function sendVonage($phone_number, $message) {
// Example for Vonage API (adjust with your Vonage credentials)
$api_key = 'your_vonage_api_key';
$api_secret = 'your_vonage_api_secret';
$from_number = 'your_vonage_phone_number';
$url = "https://rest.nexmo.com/sms/json";
$data = array(
'api_key' => $api_key,
'api_secret' => $api_secret,
'to' => $phone_number,
'from' => $from_number,
'text' => $message
);
return $this->sendRequest($url, $data);
}
// General function for sending request to any SMS gateway
private function sendRequest($url, $data, $username = null, $password = null) {
$ch = curl_init();
if ($username && $password) {
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_message = curl_error($ch);
curl_close($ch);
return array('success' => false, 'error' => $error_message);
} else {
curl_close($ch);
return array('success' => true, 'response' => $response);
}
}
}
?>
Объяснение кода:
-
Класс
SMSGateway
:-
Этот класс используется для отправки SMS-сообщений через различные SMS-шлюзы (например, SMS.ru, Twilio, Plivo, Vonage и т. д.).
-
Конструктор класса принимает три параметра:
-
$api_url
: URL для API SMS-шлюза. -
$api_key
: Ключ API для аутентификации. -
$sender_id
: ID отправителя.
-
-
-
Методы для отправки SMS:
-
sendSMS($phone_number, $message)
: Основной метод для отправки SMS-сообщений через выбранный SMS-шлюз. -
sendSMSRU($phone_number, $message)
: Метод для отправки SMS через SMS.ru. -
sendTwilio($phone_number, $message)
: Метод для отправки SMS через Twilio. -
sendPlivo($phone_number, $message)
: Метод для отправки SMS через Plivo. -
sendVonage($phone_number, $message)
: Метод для отправки SMS через Vonage.
-
-
Метод
sendRequest()
:-
Универсальный метод для отправки запроса через cURL на любой SMS-шлюз.
-
Он может работать с любыми шлюзами, используя параметры аутентификации (если требуется), URL и данные для отправки.
-
Зависимости:
-
cURL: Для выполнения HTTP-запросов.
Этот файл предоставляет функции для интеграции различных SMS-шлюзов, и он будет использоваться в модуле для отправки SMS-уведомлений пользователям в рамках "Менеджера задач".