1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
|
5
|
|
|
abstract class Agent |
6
|
|
|
{ |
7
|
|
|
const SUCCESS = 'success'; |
8
|
|
|
const INFO = 'info'; |
9
|
|
|
const CODE = 'code'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The configuration information. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $config = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The result data. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $result = [ |
24
|
|
|
self::SUCCESS => false, |
25
|
|
|
self::INFO => null, |
26
|
|
|
self::CODE => 0, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $config |
33
|
|
|
*/ |
34
|
30 |
|
public function __construct(array $config = []) |
35
|
|
|
{ |
36
|
30 |
|
$this->config($config); |
37
|
30 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get or set the configuration information of agent. |
41
|
|
|
* |
42
|
|
|
* @param mixed $key |
43
|
|
|
* @param mixed $value |
44
|
|
|
* @param bool $override |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
30 |
|
public function config($key = null, $value = null, $override = false) |
49
|
|
|
{ |
50
|
30 |
|
if (is_array($key) && is_bool($value)) { |
51
|
3 |
|
$override = $value; |
52
|
3 |
|
} |
53
|
|
|
|
54
|
30 |
|
return Util::operateArray($this->config, $key, $value, null, null, $override); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* SMS send process. |
59
|
|
|
* |
60
|
|
|
* @param $to |
61
|
|
|
* @param $content |
62
|
|
|
* @param $tempId |
63
|
|
|
* @param array $tempData |
64
|
|
|
*/ |
65
|
|
|
abstract public function sendSms($to, $content, $tempId, array $tempData); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Content SMS send process. |
69
|
|
|
* |
70
|
|
|
* @param $to |
71
|
|
|
* @param $content |
72
|
|
|
*/ |
73
|
|
|
abstract public function sendContentSms($to, $content); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Template SMS send process. |
77
|
|
|
* |
78
|
|
|
* @param $to |
79
|
|
|
* @param $tempId |
80
|
|
|
* @param array $tempData |
81
|
|
|
*/ |
82
|
|
|
abstract public function sendTemplateSms($to, $tempId, array $tempData); |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Voice verify send process. |
86
|
|
|
* |
87
|
|
|
* @param $to |
88
|
|
|
* @param $code |
89
|
|
|
* @param $tempId |
90
|
|
|
* @param array $tempData |
91
|
|
|
*/ |
92
|
|
|
abstract public function voiceVerify($to, $code, $tempId, array $tempData); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* cURl |
96
|
|
|
* |
97
|
|
|
* @codeCoverageIgnore |
98
|
|
|
* |
99
|
|
|
* @param string $url [请求的URL地址] |
100
|
|
|
* @param array $params [请求的参数] |
101
|
|
|
* @param bool $post [是否采用POST形式] |
102
|
|
|
* @param array $opts [curl设置项] |
103
|
|
|
* |
104
|
|
|
* @return array ['request', 'response'] |
105
|
|
|
* request:是否请求成功 |
106
|
|
|
* response:响应数据 |
107
|
|
|
*/ |
108
|
|
|
public static function curl($url, array $params = [], $post = false, array $opts = []) |
109
|
|
|
{ |
110
|
|
|
$request = true; |
111
|
|
|
if (is_array($post)) { |
112
|
|
|
$opts = $post; |
113
|
|
|
} |
114
|
|
|
$ch = curl_init(); |
115
|
|
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
116
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
117
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); |
118
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
119
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
120
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
121
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
122
|
|
|
if ($post) { |
123
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
124
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); |
125
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
126
|
|
|
} else { |
127
|
|
|
$params = http_build_query($params); |
128
|
|
|
curl_setopt($ch, CURLOPT_URL, $params ? "$url?$params" : $url); |
129
|
|
|
} |
130
|
|
|
foreach ($opts as $key => $value) { |
131
|
|
|
curl_setopt($ch, $key, $value); |
132
|
|
|
} |
133
|
|
|
$response = curl_exec($ch); |
134
|
|
|
if ($response === false) { |
135
|
|
|
$request = false; |
136
|
|
|
$response = curl_getinfo($ch); |
137
|
|
|
} |
138
|
|
|
curl_close($ch); |
139
|
|
|
|
140
|
|
|
return compact('request', 'response'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get or set the result data. |
145
|
|
|
* |
146
|
|
|
* @param $name |
147
|
|
|
* @param $value |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
36 |
|
public function result($name = null, $value = null) |
152
|
|
|
{ |
153
|
36 |
|
if ($name === null) { |
154
|
33 |
|
return $this->result; |
155
|
|
|
} |
156
|
33 |
|
if (array_key_exists($name, $this->result)) { |
157
|
33 |
|
if ($value === null) { |
158
|
6 |
|
return $this->result["$name"]; |
159
|
|
|
} |
160
|
33 |
|
$this->result["$name"] = $value; |
161
|
33 |
|
} |
162
|
33 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Overload object properties. |
166
|
|
|
* |
167
|
|
|
* @param $name |
168
|
|
|
* |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
12 |
|
public function __get($name) |
172
|
|
|
{ |
173
|
12 |
|
return $this->config($name); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* When using isset() or empty() on inaccessible object properties, |
178
|
|
|
* the __isset() overloading method will be called. |
179
|
|
|
* |
180
|
|
|
* @param $name |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
3 |
|
public function __isset($name) |
185
|
|
|
{ |
186
|
3 |
|
return isset($this->config[$name]); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|