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->sendContentSms($to, $content); |
|
|
|
|
17
|
|
|
$this->sendTemplateSms($to, $tempId ,$data); |
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
|
|
|
// dd($result); |
|
|
|
|
45
|
|
|
$this->setResult($result); |
46
|
|
|
} |
47
|
|
|
protected function createContentParams(array $params) |
48
|
|
|
{ |
49
|
|
|
$tel = new \stdClass(); |
50
|
|
|
// $telarr=explode('-',$params['to']); |
|
|
|
|
51
|
|
|
$tel->nationcode = '86'; |
52
|
|
|
$tel->phone = $params['to']; |
53
|
|
|
$jsondata = new \stdClass(); |
54
|
|
|
$jsondata->tel = $tel; |
55
|
|
|
$jsondata->type = "0"; |
56
|
|
|
$jsondata->msg = $params['content']; |
57
|
|
|
$jsondata->sig = md5($this->appkey.$params['to']); |
58
|
|
|
$jsondata->extend = ""; // 根据需要添加,一般保持默认 |
59
|
|
|
$jsondata->ext = ""; // 根据需要添加,一般保持默认 |
60
|
|
|
$params =json_encode($jsondata); |
61
|
|
|
return $params; |
62
|
|
|
} |
63
|
|
|
protected function createTemplateParams(array $params) |
64
|
|
|
{ |
65
|
|
|
$tel = new \stdClass(); |
66
|
|
|
// $telarr=explode('-',$params['to']); |
|
|
|
|
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
|
|
|
return $params; |
79
|
|
|
} |
80
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
protected function QcloudCurl($url, $optData) |
85
|
|
|
{ |
86
|
|
|
// dd($url,$optData); |
|
|
|
|
87
|
|
|
$ch = curl_init(); |
88
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
89
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
90
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
91
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
92
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $optData); |
93
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
94
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
95
|
|
|
$response = curl_exec($ch); |
96
|
|
|
if ($response === false) { |
97
|
|
|
$request = false; |
98
|
|
|
$response = curl_getinfo($ch); |
99
|
|
|
} |
100
|
|
|
curl_close($ch); |
101
|
|
|
$response=json_decode($response,true); |
102
|
|
|
return compact('request', 'response'); |
103
|
|
|
} |
104
|
|
|
protected function genSign($params) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
protected function setResult($result) |
109
|
|
|
{ |
110
|
|
|
if ($result['response']['result']=='0') { |
111
|
|
|
$this->result(Agent::SUCCESS, true); |
112
|
|
|
$this->result(Agent::INFO, $result['response']['ext']); |
113
|
|
|
$this->result(Agent::CODE, 1111); |
114
|
|
|
} else { |
115
|
|
|
$this->result(Agent::SUCCESS, false); |
116
|
|
|
$this->result(Agent::INFO, $result['response']['errmsg']); |
117
|
|
|
$this->result(Agent::CODE, 0000); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
protected function genResponseName($method) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function getTempDataString(array $data) |
125
|
|
|
{ |
126
|
|
|
$data = array_map(function ($value) { |
127
|
|
|
return (string) $value; |
128
|
|
|
}, $data); |
129
|
|
|
|
130
|
|
|
return json_encode($data); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.