1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Strebl\Inetworx; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Strebl\Inetworx\Exceptions\ApiErrorException; |
7
|
|
|
|
8
|
|
|
class InetworxClient |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $baseUrl = 'https://sms.inetworx.ch/'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $broadcastUrl = 'smsapp/sendsms.php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $optionsUrl = 'smsapp/options.php'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Client |
27
|
|
|
*/ |
28
|
|
|
protected $httpClient; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $apiPassword; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $apiUsername; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Inetworx Client Constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $authHeaderUsername |
44
|
|
|
* @param string $authHeaderPassword |
45
|
|
|
* @param string $apiUsername |
46
|
|
|
* @param string $apiPassword |
47
|
|
|
* @param $client |
48
|
|
|
*/ |
49
|
|
|
public function __construct($authHeaderUsername, $authHeaderPassword, $apiUsername, $apiPassword, $client = null) |
50
|
|
|
{ |
51
|
|
|
if ($client) { |
52
|
|
|
$this->httpClient = $client; |
53
|
|
|
} else { |
54
|
|
|
$this->createHttpClient($authHeaderUsername, $authHeaderPassword); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->apiUsername = $apiUsername; |
58
|
|
|
$this->apiPassword = $apiPassword; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Send the message. |
63
|
|
|
* |
64
|
|
|
* @param $phoneNumber |
65
|
|
|
* @param $message |
66
|
|
|
* @param $fromNumber |
67
|
|
|
* |
68
|
|
|
* @return array $response |
69
|
|
|
*/ |
70
|
|
|
public function send($phoneNumber, $message, $fromNumber) |
71
|
|
|
{ |
72
|
|
|
return $this->sendRequest($this->getBroadcastUrl(), [ |
73
|
|
|
'sender' => $fromNumber, |
74
|
|
|
'rcpt' => $phoneNumber, |
75
|
|
|
'msgbody' => $message, |
76
|
|
|
'coding' => 2, |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the remaining SMS credits. |
82
|
|
|
* |
83
|
|
|
* @throws ApiErrorException |
84
|
|
|
* |
85
|
|
|
* @return int $credit |
86
|
|
|
*/ |
87
|
|
|
public function credit() |
88
|
|
|
{ |
89
|
|
|
return (int) $this->sendRequest($this->getOptionsUrl(), ['option' => 'quota'])['body']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $url |
94
|
|
|
* @param $params |
95
|
|
|
* |
96
|
|
|
* @throws ApiErrorException |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function sendRequest($url, $params) |
101
|
|
|
{ |
102
|
|
|
$response = $this |
103
|
|
|
->getHttpClient() |
104
|
|
|
->post($url, [ |
105
|
|
|
'form_params' => array_merge([ |
106
|
|
|
'user' => $this->apiUsername, |
107
|
|
|
'pass' => $this->apiPassword, |
108
|
|
|
], $params), |
109
|
|
|
]); |
110
|
|
|
|
111
|
|
|
if ($response->getStatusCode() !== 200 || $this->responseCodeFromBody($response) !== 200) { |
112
|
|
|
throw new ApiErrorException( |
113
|
|
|
$this->responseBody($response) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return [ |
118
|
|
|
'body' => $this->responseBody($response), |
119
|
|
|
'full_body' => (string) $response->getBody(), |
120
|
|
|
'status' => $response->getStatusCode(), |
121
|
|
|
'message' => $response->getReasonPhrase(), |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* The Inetworx API doesn't respond with a useful HTTP Status Code if the request fails. |
127
|
|
|
* It puts the status code into the response body. |
128
|
|
|
* for example: 404: Invalid Option. |
129
|
|
|
* |
130
|
|
|
* @return int $code |
131
|
|
|
*/ |
132
|
|
|
protected function responseCodeFromBody($response) |
133
|
|
|
{ |
134
|
|
|
return (int) explode(':', (string) $response->getBody())[0]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Inetworx puts the status code in the response body too. |
139
|
|
|
* Because of that, we have to parse the answer. |
140
|
|
|
* |
141
|
|
|
* @param $response |
142
|
|
|
* |
143
|
|
|
* @return string $body |
144
|
|
|
*/ |
145
|
|
|
protected function responseBody($response) |
146
|
|
|
{ |
147
|
|
|
return trim(explode(':', (string) $response->getBody())[1]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getBaseUrl() |
154
|
|
|
{ |
155
|
|
|
return $this->baseUrl; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $baseUrl |
160
|
|
|
*/ |
161
|
|
|
public function setBaseUrl($baseUrl) |
162
|
|
|
{ |
163
|
|
|
$this->baseUrl = $baseUrl; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getBroadcastUrl() |
170
|
|
|
{ |
171
|
|
|
return $this->broadcastUrl; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $broadcastUrl |
176
|
|
|
*/ |
177
|
|
|
public function setBroadcastUrl($broadcastUrl) |
178
|
|
|
{ |
179
|
|
|
$this->broadcastUrl = $broadcastUrl; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getOptionsUrl() |
186
|
|
|
{ |
187
|
|
|
return $this->optionsUrl; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $optionsUrl |
192
|
|
|
*/ |
193
|
|
|
public function setOptionsUrl($optionsUrl) |
194
|
|
|
{ |
195
|
|
|
$this->optionsUrl = $optionsUrl; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return Client |
200
|
|
|
*/ |
201
|
|
|
protected function getHttpClient() |
202
|
|
|
{ |
203
|
|
|
return $this->httpClient; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param $client |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
protected function setHttpClient($client) |
212
|
|
|
{ |
213
|
|
|
$this->httpClient = $client; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
protected function createHttpClient($authHeaderUsername, $authHeaderPassword) |
220
|
|
|
{ |
221
|
|
|
$this->httpClient = new Client([ |
222
|
|
|
'base_uri' => $this->getBaseUrl(), |
223
|
|
|
'auth' => [ |
224
|
|
|
$authHeaderUsername, |
225
|
|
|
$authHeaderPassword, |
226
|
|
|
], |
227
|
|
|
]); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|