1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2015 Topface, LLC <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace TopfaceLibrary\SmsOnline\Bulk; |
19
|
|
|
|
20
|
|
|
use alxmsl\Network\Http\Request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Sms Bulk API client |
24
|
|
|
* @author alxmsl |
25
|
|
|
*/ |
26
|
|
|
final class Client { |
27
|
|
|
/** |
28
|
|
|
* Endpoint url for Sms Bulk API |
29
|
|
|
*/ |
30
|
|
|
const ENDPOINT_URI = 'https://bulk.sms-online.com'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string sender alpha-name |
34
|
|
|
*/ |
35
|
|
|
private $from = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string secret key |
39
|
|
|
*/ |
40
|
|
|
private $secret = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string sender login |
44
|
|
|
*/ |
45
|
|
|
private $user = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int connect timeout, seconds |
49
|
|
|
*/ |
50
|
|
|
private $connectTimeout = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var int request timeout, seconds |
54
|
|
|
*/ |
55
|
|
|
private $requestTimeout = 0; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param int $connectTimeout connect timeout, seconds |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
1 |
|
public function setConnectTimeout($connectTimeout) { |
62
|
1 |
|
$this->connectTimeout = (int) $connectTimeout; |
63
|
1 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return int connect timeout, seconds |
68
|
|
|
*/ |
69
|
2 |
|
public function getConnectTimeout() { |
70
|
2 |
|
return $this->connectTimeout; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param int $requestTimeout request timeout, seconds |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
1 |
|
public function setRequestTimeout($requestTimeout) { |
78
|
1 |
|
$this->requestTimeout = (int) $requestTimeout; |
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return int request timeout, seconds |
84
|
|
|
*/ |
85
|
2 |
|
public function getRequestTimeout() { |
86
|
2 |
|
return $this->requestTimeout; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $from sender alpha-name |
91
|
|
|
* @param string $user sender login |
92
|
|
|
* @param string $secret secret key |
93
|
|
|
*/ |
94
|
3 |
|
public function __construct($from, $user, $secret) { |
95
|
3 |
|
$this->from = (string) $from; |
96
|
3 |
|
$this->secret = (string) $secret; |
97
|
3 |
|
$this->user = (string) $user; |
98
|
3 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Send message method |
102
|
|
|
* @param Message $Message message instance |
103
|
|
|
* @return Response response instance |
104
|
|
|
* @codeIgnoreCoverage |
105
|
|
|
*/ |
106
|
|
|
public function send(Message $Message) { |
107
|
|
|
return Response::initializeByString($this->createRequest($Message)->send()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create request method |
112
|
|
|
* @param Message $Message message instance |
113
|
|
|
* @return Request HTTP request instance |
114
|
|
|
*/ |
115
|
1 |
|
private function createRequest(Message $Message) { |
116
|
1 |
|
$Request = new Request(); |
117
|
1 |
|
$Request->setTransport(Request::TRANSPORT_CURL); |
118
|
1 |
|
$Request->setUrl(self::ENDPOINT_URI) |
119
|
1 |
|
->setContentTypeCode(Request::CONTENT_TYPE_TEXT) |
120
|
1 |
|
->setConnectTimeout($this->getConnectTimeout()) |
121
|
1 |
|
->setTimeout($this->getRequestTimeout()); |
122
|
|
|
|
123
|
1 |
|
$data = array_merge([ |
124
|
1 |
|
'user=' . $this->user, |
125
|
1 |
|
'from=' . $this->from, |
126
|
1 |
|
'sign=' . $this->sign($Message), |
127
|
1 |
|
], $Message->export()); |
128
|
1 |
|
$Request->setPostData(implode('&', $data)); |
129
|
1 |
|
return $Request; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sign message method |
134
|
|
|
* @param Message $Message message instance |
135
|
|
|
* @return string message signature |
136
|
|
|
*/ |
137
|
2 |
|
private function sign(Message $Message) { |
138
|
2 |
|
return md5($this->user . $this->from . implode($Message->getPhones()) . $Message->getText() . $this->secret); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|