1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Every8d; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use DomainException; |
7
|
|
|
use Http\Client\HttpClient; |
8
|
|
|
use Http\Message\MessageFactory; |
9
|
|
|
use Http\Discovery\HttpClientDiscovery; |
10
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
11
|
|
|
|
12
|
|
|
class Client |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* $apiEndpoint. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $apiEndpoint = 'http://api.every8d.com/API21/HTTP'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* $credit. |
23
|
|
|
* |
24
|
|
|
* @var float |
25
|
|
|
*/ |
26
|
|
|
public $credit = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* $userId. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $userId; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* $password. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $password; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* $httpClient. |
44
|
|
|
* |
45
|
|
|
* @var \Http\Client\HttpClient |
46
|
|
|
*/ |
47
|
|
|
protected $httpClient; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* $messageFactory. |
51
|
|
|
* |
52
|
|
|
* @var \Http\Message\MessageFactory |
53
|
|
|
*/ |
54
|
|
|
protected $messageFactory; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* __construct. |
58
|
|
|
* |
59
|
|
|
* @param string $userId |
60
|
|
|
* @param string $password |
61
|
|
|
* @param \Http\Client\HttpClient $httpClient |
62
|
|
|
* @param \Http\Message\MessageFactory $messageFactory |
63
|
|
|
*/ |
64
|
6 |
|
public function __construct($userId, $password, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
65
|
|
|
{ |
66
|
6 |
|
$this->userId = $userId; |
67
|
6 |
|
$this->password = $password; |
68
|
6 |
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
69
|
6 |
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
70
|
6 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* https. |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
2 |
|
public function https() |
78
|
|
|
{ |
79
|
2 |
|
$this->apiEndpoint = 'https://oms.every8d.com/API21/HTTP/'; |
80
|
|
|
|
81
|
2 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* send. |
86
|
|
|
* |
87
|
|
|
* @param array $params |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public function send($params) |
91
|
|
|
{ |
92
|
2 |
|
$response = $this->doRequest('sendSMS.ashx', array_filter(array_merge([ |
93
|
2 |
|
'UID' => $this->userId, |
94
|
2 |
|
'PWD' => $this->password, |
95
|
|
|
'SB' => null, |
96
|
|
|
'MSG' => null, |
97
|
|
|
'DEST' => null, |
98
|
|
|
'ST' => null, |
99
|
|
|
'RETRYTIME' => null, |
100
|
2 |
|
], $this->remapParams($params)))); |
101
|
|
|
|
102
|
2 |
|
if ($this->isValidResponse($response) === false) { |
103
|
1 |
|
throw new DomainException($response, 500); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $this->parseResponse($response); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* sendMMS. |
111
|
|
|
* |
112
|
|
|
* @param array $params |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
1 |
|
public function sendMMS($params) |
116
|
|
|
{ |
117
|
1 |
|
$response = $this->https()->doRequest('snedMMS.ashx', array_filter(array_merge([ |
118
|
1 |
|
'UID' => $this->userId, |
119
|
1 |
|
'PWD' => $this->password, |
120
|
|
|
'SB' => null, |
121
|
|
|
'MSG' => null, |
122
|
|
|
'DEST' => null, |
123
|
|
|
'ST' => null, |
124
|
|
|
'RETRYTIME' => null, |
125
|
|
|
'ATTACHMENT' => null, |
126
|
|
|
'TYPE' => null, |
127
|
1 |
|
], $this->remapParams($params)))); |
128
|
|
|
|
129
|
1 |
|
if ($this->isValidResponse($response) === false) { |
130
|
|
|
throw new DomainException($response, 500); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return $this->parseResponse($response); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* credit. |
138
|
|
|
* |
139
|
|
|
* @return float |
140
|
|
|
*/ |
141
|
4 |
|
public function credit() |
142
|
|
|
{ |
143
|
4 |
|
if (is_null($this->credit) === false) { |
144
|
2 |
|
return $this->credit; |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
$response = $this->doRequest('getCredit.ashx', [ |
148
|
2 |
|
'UID' => $this->userId, |
149
|
2 |
|
'PWD' => $this->password, |
150
|
|
|
]); |
151
|
|
|
|
152
|
2 |
|
if ($this->isValidResponse($response) === false) { |
153
|
1 |
|
throw new DomainException($response, 500); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $this->setCredit($response)->credit; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* setCredit. |
161
|
|
|
* |
162
|
|
|
* @param string $credit |
163
|
|
|
*/ |
164
|
3 |
|
protected function setCredit($credit) |
165
|
|
|
{ |
166
|
3 |
|
$this->credit = (float) $credit; |
167
|
|
|
|
168
|
3 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* isValidResponse. |
173
|
|
|
* |
174
|
|
|
* @param string $response |
175
|
|
|
* |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
5 |
|
protected function isValidResponse($response) |
179
|
|
|
{ |
180
|
5 |
|
return substr($response, 0, 1) !== '-'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* doRequest. |
185
|
|
|
* |
186
|
|
|
* @param string $uri |
187
|
|
|
* @param array $params |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
5 |
|
protected function doRequest($uri, $params) |
192
|
|
|
{ |
193
|
5 |
|
$request = $this->messageFactory->createRequest( |
194
|
5 |
|
'POST', |
195
|
5 |
|
rtrim($this->apiEndpoint, '/').'/'.$uri, |
196
|
5 |
|
['Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'], |
197
|
5 |
|
http_build_query($params) |
198
|
|
|
); |
199
|
5 |
|
$response = $this->httpClient->sendRequest($request); |
200
|
|
|
|
201
|
5 |
|
return $response->getBody()->getContents(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* remapParams. |
206
|
|
|
* |
207
|
|
|
* @param array $params |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
3 |
|
protected function remapParams($params) |
211
|
|
|
{ |
212
|
3 |
|
if (empty($params['subject']) === false) { |
213
|
|
|
$params['SB'] = $params['subject']; |
214
|
|
|
unset($params['subject']); |
215
|
|
|
} |
216
|
|
|
|
217
|
3 |
View Code Duplication |
if (empty($params['to']) === false) { |
|
|
|
|
218
|
3 |
|
$params['DEST'] = $params['to']; |
219
|
3 |
|
unset($params['to']); |
220
|
|
|
} |
221
|
|
|
|
222
|
3 |
View Code Duplication |
if (empty($params['text']) === false) { |
|
|
|
|
223
|
3 |
|
$params['MSG'] = $params['text']; |
224
|
3 |
|
unset($params['text']); |
225
|
|
|
} |
226
|
|
|
|
227
|
3 |
|
if (empty($params['sendTime']) === false) { |
228
|
2 |
|
$params['ST'] = empty($params['sendTime']) === false ? Carbon::parse($params['sendTime'])->format('YmdHis') : null; |
229
|
2 |
|
unset($params['sendTime']); |
230
|
|
|
} |
231
|
|
|
|
232
|
3 |
View Code Duplication |
if (empty($params['attachment']) === false) { |
|
|
|
|
233
|
1 |
|
$params['ATTACHMENT'] = $params['attachment']; |
234
|
1 |
|
unset($params['attachment']); |
235
|
|
|
} |
236
|
|
|
|
237
|
3 |
View Code Duplication |
if (empty($params['type']) === false) { |
|
|
|
|
238
|
1 |
|
$params['TYPE'] = $params['type']; |
239
|
1 |
|
unset($params['type']); |
240
|
|
|
} |
241
|
|
|
|
242
|
3 |
|
return $params; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* parseResponse. |
247
|
|
|
* |
248
|
|
|
* @param array $response |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
2 |
|
protected function parseResponse($response) |
252
|
|
|
{ |
253
|
2 |
|
list($credit, $sended, $cost, $unsend, $batchId) = explode(',', $response); |
254
|
|
|
|
255
|
|
|
return [ |
256
|
2 |
|
'credit' => $this->setCredit($credit)->credit, |
257
|
2 |
|
'sended' => (int) $sended, |
258
|
2 |
|
'cost' => (float) $cost, |
259
|
2 |
|
'unsend' => (int) $unsend, |
260
|
2 |
|
'batchId' => $batchId, |
261
|
|
|
]; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: