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
|
|
|
|
15
|
|
|
public function sendSms($to, $content, $tempId, array $data) |
16
|
|
|
{ |
17
|
|
|
$this->sendTemplateSms($to, $tempId, $data); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public function sendContentSms($to, $content) |
22
|
|
|
{ |
23
|
|
|
$params['to']=$to; |
|
|
|
|
24
|
|
|
$params['content']=$content; |
25
|
|
|
$this->request($params); |
26
|
|
|
} |
27
|
|
|
public function sendTemplateSms($to, $tempId, array $data) |
28
|
|
|
{ |
29
|
|
|
$params['to'] = $to; |
|
|
|
|
30
|
|
|
$params['tempId']=(int)$tempId; |
31
|
|
|
$params['tempdata']=$data; |
32
|
|
|
$this->request($params); |
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
|
|
|
protected function createContentParams(array $params) |
47
|
|
|
{ |
48
|
|
|
$tel = new \stdClass(); |
49
|
|
|
$tel->nationcode = '86'; |
50
|
|
|
$tel->phone = $params['to']; |
51
|
|
|
$jsondata = new \stdClass(); |
52
|
|
|
$jsondata->tel = $tel; |
53
|
|
|
$jsondata->type = "0"; |
54
|
|
|
$jsondata->msg = $params['content']; |
55
|
|
|
$jsondata->sig = md5($this->appkey.$params['to']); |
56
|
|
|
$jsondata->extend = ""; // 根据需要添加,一般保持默认 |
57
|
|
|
$jsondata->ext = ""; // 根据需要添加,一般保持默认 |
58
|
|
|
$params =json_encode($jsondata); |
59
|
|
|
return $params; |
60
|
|
|
} |
61
|
|
|
protected function createTemplateParams(array $params) |
62
|
|
|
{ |
63
|
|
|
$tel = new \stdClass(); |
64
|
|
|
$tel->nationcode = '86'; |
65
|
|
|
$tel->phone = $params['to']; |
66
|
|
|
$jsondata = new \stdClass(); |
67
|
|
|
$jsondata->tel = $tel; |
68
|
|
|
$jsondata->type = '0'; |
69
|
|
|
$jsondata->tpl_id=$params['tempId']; |
70
|
|
|
$jsondata->params=[$params['tempdata']['code'],(string)$params['tempdata']['minutes']]; |
71
|
|
|
$jsondata->sig = md5($this->appkey.$params['to']); |
72
|
|
|
$jsondata->extend = ""; // 根据需要添加,一般保持默认 |
73
|
|
|
$jsondata->ext = ""; // 根据需要添加,一般保持默认 |
74
|
|
|
$params =json_encode($jsondata); |
75
|
|
|
return $params; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
79
|
|
|
{ |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
protected function QcloudCurl($url, $optData) |
83
|
|
|
{ |
84
|
|
|
$ch = curl_init(); |
85
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
86
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
87
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
88
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
89
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $optData); |
90
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
91
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
92
|
|
|
$response = curl_exec($ch); |
93
|
|
|
if ($response === false) { |
94
|
|
|
$request = false; |
95
|
|
|
$response = curl_getinfo($ch); |
96
|
|
|
} |
97
|
|
|
curl_close($ch); |
98
|
|
|
$response=json_decode($response, true); |
99
|
|
|
return compact('request', 'response'); |
100
|
|
|
} |
101
|
|
|
protected function genSign($params) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
// |
104
|
|
|
} |
105
|
|
|
protected function setResult($result) |
106
|
|
|
{ |
107
|
|
|
if ($result['response']['result']=='0') { |
108
|
|
|
$this->result(Agent::SUCCESS, true); |
109
|
|
|
$this->result(Agent::INFO, $result['response']['ext']); |
110
|
|
|
$this->result(Agent::CODE, 1111); |
111
|
|
|
} else { |
112
|
|
|
$this->result(Agent::SUCCESS, false); |
113
|
|
|
$this->result(Agent::INFO, $result['response']['errmsg']); |
114
|
|
|
$this->result(Agent::CODE, 0000); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
protected function genResponseName($method) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getTempDataString(array $data) |
122
|
|
|
{ |
123
|
|
|
$data = array_map(function ($value) { |
124
|
|
|
return (string) $value; |
125
|
|
|
}, $data); |
126
|
|
|
|
127
|
|
|
return json_encode($data); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.