-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOfon.php
More file actions
executable file
·144 lines (118 loc) · 4.17 KB
/
Copy pathPlusOfon.php
File metadata and controls
executable file
·144 lines (118 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Pet\Module;
class PlusOfon
{
private const CLIENT_ID = '10553';
private const URL = 'https://restapi.plusofon.ru/api/v1';
private const NUMBER_ID = 106561;
public function __construct(
private readonly string $token,
) {
}
/**
* Отправка SMS сообщения.
*
* @return array{success: bool, id?: string, error?: string, status?: int, details?: mixed}
*/
public function sms(string|int $phone, string $text): array
{
try {
$to = $this->normPhone($phone);
$toNum = filter_var($to, FILTER_VALIDATE_INT);
if ($to === '' || $toNum === false) {
return ['success' => false, 'error' => 'PlusOfon: неверный номер получателя'];
}
$requestBody = [
'text' => $text,
'number_id' => self::NUMBER_ID,
'to' => $toNum,
'reject_long' => true,
'count_pdu' => true,
];
[$status, $bodyText] = $this->post('/sms', $requestBody);
if ($status < 200 || $status >= 300) {
$bodyJson = $this->tryJson($bodyText);
$message = (is_array($bodyJson) && isset($bodyJson['message']) && is_string($bodyJson['message']))
? $bodyJson['message']
: "PlusOfon HTTP {$status}";
return [
'success' => false,
'status' => $status,
'error' => $message,
'details' => $bodyJson ?? $bodyText,
];
}
$data = $this->tryJson($bodyText);
if (is_array($data) && !empty($data['success'])) {
return [
'success' => true,
'id' => (string) ($data['data']['id'] ?? ''),
];
}
return [
'success' => false,
'status' => $status,
'error' => 'PlusOfon: отправка неуспешна',
'details' => $data ?? $bodyText,
];
} catch (\Throwable $error) {
error_log('Ошибка отправки SMS: ' . $error->getMessage());
return [
'success' => false,
'error' => $error->getMessage(),
];
}
}
private function normPhone(string|int $phone): string
{
$digits = preg_replace('/\D/', '', (string) $phone) ?? '';
if (strlen($digits) === 11 && str_starts_with($digits, '8')) {
return '7' . substr($digits, 1);
}
if (strlen($digits) === 10) {
return '7' . $digits;
}
return $digits;
}
/**
* @return array{0: int, 1: string}
*/
private function post(string $path, array $body): array
{
$ch = curl_init(self::URL . $path);
if ($ch === false) {
throw new \RuntimeException('PlusOfon: не удалось инициализировать HTTP-клиент');
}
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'Client: ' . self::CLIENT_ID,
'Authorization: Bearer ' . $this->token,
],
CURLOPT_POSTFIELDS => json_encode($body, JSON_THROW_ON_ERROR),
]);
$bodyText = curl_exec($ch);
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($bodyText === false) {
$error = curl_error($ch);
curl_close($ch);
throw new \RuntimeException($error !== '' ? $error : 'PlusOfon: ошибка HTTP-запроса');
}
curl_close($ch);
return [$status, $bodyText];
}
private function tryJson(string $value): mixed
{
if ($value === '') {
return null;
}
try {
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
return null;
}
}
}