|
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
|
4 |
|
public function __construct($userId, $password, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
|
65
|
|
|
{ |
|
66
|
4 |
|
$this->userId = $userId; |
|
67
|
4 |
|
$this->password = $password; |
|
68
|
4 |
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
|
69
|
4 |
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
|
70
|
4 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* setCredit. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $credit |
|
76
|
|
|
*/ |
|
77
|
2 |
|
protected function setCredit($credit) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$this->credit = (float) $credit; |
|
80
|
|
|
|
|
81
|
2 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* credit. |
|
86
|
|
|
* |
|
87
|
|
|
* @return float |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function credit() |
|
90
|
|
|
{ |
|
91
|
3 |
|
if (is_null($this->credit) === false) { |
|
92
|
1 |
|
return $this->credit; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
$response = $this->doRequest('getCredit.ashx', [ |
|
96
|
2 |
|
'UID' => $this->userId, |
|
97
|
2 |
|
'PWD' => $this->password, |
|
98
|
2 |
|
]); |
|
99
|
|
|
|
|
100
|
2 |
|
if ($this->isValidResponse($response) === false) { |
|
101
|
1 |
|
throw new DomainException($response, 500); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return $this->setCredit($response) |
|
105
|
1 |
|
->credit; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* send. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $params |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function send($params) |
|
116
|
|
|
{ |
|
117
|
2 |
|
$response = $this->doRequest('sendSMS.ashx', array_filter([ |
|
118
|
2 |
|
'UID' => $this->userId, |
|
119
|
2 |
|
'PWD' => $this->password, |
|
120
|
2 |
|
'SB' => isset($params['subject']) ? $params['subject'] : null, |
|
121
|
2 |
|
'MSG' => $params['text'], |
|
122
|
2 |
|
'DEST' => $params['to'], |
|
123
|
2 |
|
'ST' => isset($params['ST']) ? Carbon::parse($params['ST'])->format('YmdHis') : null, |
|
124
|
2 |
|
])); |
|
125
|
|
|
|
|
126
|
2 |
|
if ($this->isValidResponse($response) === false) { |
|
127
|
1 |
|
throw new DomainException($response, 500); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
list($credit, $sended, $cost, $unsend, $batchId) = explode(',', $response); |
|
131
|
|
|
|
|
132
|
|
|
return [ |
|
133
|
1 |
|
'credit' => $this->setCredit($credit)->credit, |
|
134
|
1 |
|
'sended' => (int) $sended, |
|
135
|
1 |
|
'cost' => (float) $cost, |
|
136
|
1 |
|
'unsend' => (int) $unsend, |
|
137
|
1 |
|
'batchId' => $batchId, |
|
138
|
1 |
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* isValidResponse. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $response |
|
145
|
|
|
* |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
4 |
|
protected function isValidResponse($response) |
|
149
|
|
|
{ |
|
150
|
4 |
|
return substr($response, 0, 1) !== '-'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* doRequest. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $uri |
|
157
|
|
|
* @param array $params |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
4 |
|
protected function doRequest($uri, $params) |
|
162
|
|
|
{ |
|
163
|
4 |
|
$request = $this->messageFactory->createRequest( |
|
164
|
4 |
|
'POST', |
|
165
|
4 |
|
rtrim($this->apiEndpoint, '/').'/'.$uri, |
|
166
|
4 |
|
['Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'], |
|
167
|
4 |
|
http_build_query($params) |
|
168
|
4 |
|
); |
|
169
|
4 |
|
$response = $this->httpClient->sendRequest($request); |
|
170
|
|
|
|
|
171
|
4 |
|
return $response->getBody()->getContents(); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|