Explorar el Código

upload project

Dmitriev Roman hace 10 meses
padre
commit
549c4a2001
Se han modificado 2 ficheros con 99 adiciones y 0 borrados
  1. 33 0
      inc/sms.php
  2. 66 0
      send3.php

+ 33 - 0
inc/sms.php

@@ -0,0 +1,33 @@
+<?php
+class Sms
+{
+    private static $login = 'oem@example.com';
+    private static $key   = 'password_key';
+    private static $from  = 'OEM';
+    private static $log_file = '/var/www/spsend/logs/sms.log';
+
+    public static function send($number, $text)
+    {
+        $log = date('Y-m-d H:i:s');
+	$result = array();
+	if (!empty($number)) {
+	    $numbers = explode(',', $number);
+	    foreach ($numbers as $row) {
+		$row = preg_replace('/[^0-9]/', '', $row);
+		if (!empty($row)) {
+		    $ch = curl_init();
+		    curl_setopt($ch, CURLOPT_HEADER, 0);
+		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
+		    curl_setopt($ch, CURLOPT_USERPWD, self::$login . ':' . self::$key);
+		    curl_setopt($ch, CURLOPT_URL, 'https://gate.smsaero.ru/v2/sms/send?number=' . urlencode($row) . '&text=' . urlencode($text) . '&sign=' . self::$from);
+		    $res = curl_exec($ch);
+		    curl_close($ch);
+		    $log .= $res;
+		    $result[$row]=$res;
+		    file_put_contents(self::$log_file, $log . "\r\n\r\n", FILE_APPEND);
+		}
+	    }
+	} else { return; }
+	return $result;
+    }
+}

+ 66 - 0
send3.php

@@ -0,0 +1,66 @@
+<?php
+
+require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/sms.php';
+
+/**
+ * Отправка SMS-сообщения на указанный номер.
+ *
+ * @param string $phone Номер телефона получателя (в любом формате).
+ * @param string $text Текст сообщения.
+ * @return array|int Возвращает массив со статусом отправки или 0 в случае ошибки.
+ */
+function send_sms($phone, $text) {
+    // Очищаем номер телефона от всех символов, кроме цифр
+    $phone = preg_replace('~\D+~', '', $phone);
+
+    if (!preg_match('/^[0-9]{10,13}$/', $phone)) {
+        return 0;
+    }
+
+    $len = strlen($phone);
+
+    // Преобразуем к международному формату
+    if ($len === 10) {
+        $phone = '7' . $phone;
+    } elseif ($len === 11 && preg_match('/^89/', $phone)) {
+        $phone = preg_replace('/^8/', '7', $phone);
+    } elseif ($len < 10 || $len > 13) {
+        return 0;
+    }
+
+    return Sms::send($phone, $text);
+}
+
+/**
+ * Получает параметр из POST или GET (с приоритетом POST).
+ *
+ * @param string $key Название параметра.
+ * @return string|null
+ */
+function get_request_param($key) {
+    return filter_input(INPUT_POST, $key) ?? filter_input(INPUT_GET, $key);
+}
+
+// Получаем параметры запроса
+$phone = get_request_param('to');
+$sms_text = get_request_param('text');
+
+// Если оба параметра присутствуют
+if (!empty($phone) && !empty($sms_text)) {
+    $result = send_sms($phone, $sms_text);
+
+    if (!empty($result) && is_array($result)) {
+        foreach ($result as $recipient => $statusJson) {
+            $status = json_decode($statusJson, true, 512, JSON_OBJECT_AS_ARRAY);
+            if (isset($status["data"]["id"], $status["data"]["status"])) {
+                echo htmlspecialchars($recipient) . ':' .
+                     htmlspecialchars($status["data"]["id"]) . ':' .
+                     htmlspecialchars($status["data"]["status"]) . '<br>';
+            }
+        }
+    }
+}
+
+// Очистка переменных (необязательно, но на всякий случай)
+unset($_GET, $_POST);
+?>