|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class AlidayuAgent |
|
7
|
|
|
* |
|
8
|
|
|
* @property string $appid |
|
9
|
|
|
* @property string $appkey |
|
10
|
|
|
* @property string $sendUrl |
|
11
|
|
|
*/ |
|
12
|
|
|
class QcloudAgent extends Agent |
|
13
|
|
|
{ |
|
14
|
|
|
public function sendSms($to, $content, $tempId, array $data) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->sendTemplateSms($to, $tempId, $data); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function sendContentSms($to, $content) |
|
20
|
|
|
{ |
|
21
|
|
|
$params['to'] = $to; |
|
|
|
|
|
|
22
|
|
|
$params['content'] = $content; |
|
23
|
|
|
$this->request($params); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function sendTemplateSms($to, $tempId, array $data) |
|
27
|
|
|
{ |
|
28
|
|
|
$params['to'] = $to; |
|
|
|
|
|
|
29
|
|
|
$params['tempId'] = (int) $tempId; |
|
30
|
|
|
$params['tempdata'] = $data; |
|
31
|
|
|
$this->request($params); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function request(array $params) |
|
35
|
|
|
{ |
|
36
|
|
|
$randNum = rand(100000, 999999); |
|
37
|
|
|
$sendUrl = $this->sendUrl . '?sdkappid=' . $this->appid . '&random=' . $randNum; |
|
38
|
|
|
if (isset($params['content'])) { |
|
39
|
|
|
$params = $this->createContentParams($params); |
|
40
|
|
|
} elseif (isset($params['tempId'])) { |
|
41
|
|
|
$params = $this->createTemplateParams($params); |
|
42
|
|
|
} |
|
43
|
|
|
$result = $this->QcloudCurl($sendUrl, $params); |
|
44
|
|
|
$this->setResult($result); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function createContentParams(array $params) |
|
48
|
|
|
{ |
|
49
|
|
|
$tel = new \stdClass(); |
|
50
|
|
|
$tel->nationcode = '86'; |
|
51
|
|
|
$tel->phone = $params['to']; |
|
52
|
|
|
$jsondata = new \stdClass(); |
|
53
|
|
|
$jsondata->tel = $tel; |
|
54
|
|
|
$jsondata->type = '0'; |
|
55
|
|
|
$jsondata->msg = $params['content']; |
|
56
|
|
|
$jsondata->sig = md5($this->appkey . $params['to']); |
|
57
|
|
|
$jsondata->extend = ''; // 根据需要添加,一般保持默认 |
|
58
|
|
|
$jsondata->ext = ''; // 根据需要添加,一般保持默认 |
|
59
|
|
|
$params = json_encode($jsondata); |
|
60
|
|
|
|
|
61
|
|
|
return $params; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function createTemplateParams(array $params) |
|
65
|
|
|
{ |
|
66
|
|
|
$tel = new \stdClass(); |
|
67
|
|
|
$tel->nationcode = '86'; |
|
68
|
|
|
$tel->phone = $params['to']; |
|
69
|
|
|
$jsondata = new \stdClass(); |
|
70
|
|
|
$jsondata->tel = $tel; |
|
71
|
|
|
$jsondata->type = '0'; |
|
72
|
|
|
$jsondata->tpl_id = $params['tempId']; |
|
73
|
|
|
$jsondata->params = [$params['tempdata']['code'], (string) $params['tempdata']['minutes']]; |
|
74
|
|
|
$jsondata->sig = md5($this->appkey . $params['to']); |
|
75
|
|
|
$jsondata->extend = ''; // 根据需要添加,一般保持默认 |
|
76
|
|
|
$jsondata->ext = ''; // 根据需要添加,一般保持默认 |
|
77
|
|
|
$params = json_encode($jsondata); |
|
78
|
|
|
|
|
79
|
|
|
return $params; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
|
83
|
|
|
{ |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function QcloudCurl($url, $optData) |
|
87
|
|
|
{ |
|
88
|
|
|
$ch = curl_init(); |
|
89
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
90
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
91
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
92
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
93
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $optData); |
|
94
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
|
95
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
|
96
|
|
|
$response = curl_exec($ch); |
|
97
|
|
|
if ($response === false) { |
|
98
|
|
|
$request = false; |
|
99
|
|
|
$response = curl_getinfo($ch); |
|
100
|
|
|
} |
|
101
|
|
|
curl_close($ch); |
|
102
|
|
|
$response = json_decode($response, true); |
|
103
|
|
|
|
|
104
|
|
|
return compact('request', 'response'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function genSign($params) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
// |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function setResult($result) |
|
113
|
|
|
{ |
|
114
|
|
|
if ($result['response']['result'] === '0') { |
|
115
|
|
|
$this->result(Agent::SUCCESS, true); |
|
116
|
|
|
$this->result(Agent::INFO, $result['response']['ext']); |
|
117
|
|
|
$this->result(Agent::CODE, 1111); |
|
118
|
|
|
} else { |
|
119
|
|
|
$this->result(Agent::SUCCESS, false); |
|
120
|
|
|
$this->result(Agent::INFO, $result['response']['errmsg']); |
|
121
|
|
|
$this->result(Agent::CODE, 0000); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function genResponseName($method) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
protected function getTempDataString(array $data) |
|
130
|
|
|
{ |
|
131
|
|
|
$data = array_map(function ($value) { |
|
132
|
|
|
return (string) $value; |
|
133
|
|
|
}, $data); |
|
134
|
|
|
|
|
135
|
|
|
return json_encode($data); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.