1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
/** |
5
|
|
|
* Class SendCloudAgent |
6
|
|
|
* |
7
|
|
|
* @property string $smsUser |
8
|
|
|
* @property string $smsKey |
9
|
|
|
*/ |
10
|
|
|
class QcloudAgent extends Agent implements TemplateSms |
11
|
|
|
{ |
12
|
|
|
protected $sendUrl = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms'; |
13
|
|
|
protected $random; |
14
|
|
|
protected $content; |
15
|
|
|
public function sendSms($to, $content, $tempId, array $data) |
16
|
|
|
{ |
17
|
|
|
$this->content = $content; |
18
|
|
|
$this->sendTemplateSms($to, $tempId, $data); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function sendTemplateSms($to, $tempId, array $data) |
22
|
|
|
{ |
23
|
|
|
$params = [ |
24
|
|
|
'type' => 0,//0:普通短信;1:营销短信 |
|
|
|
|
25
|
|
|
'msg' => $this->content,//$this->getTempDataString($data) |
|
|
|
|
26
|
|
|
'tel' => ["nationcode"=> "86","mobile"=>$to], |
27
|
|
|
'time' => time(), |
28
|
|
|
'extend' => "", |
29
|
|
|
'ext' => "", |
30
|
|
|
]; |
31
|
|
|
$this->random = $this->getRandom(); |
32
|
|
|
$sendUrl = $this->sendUrl.'?'.'sdkappid='.$this->appId.'&random='.$this->random; |
|
|
|
|
33
|
|
|
$this->request($sendUrl, $params); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
37
|
|
|
{ |
38
|
|
|
$params = [ |
39
|
|
|
'phone' => $to, |
40
|
|
|
'code' => $code, |
41
|
|
|
]; |
42
|
|
|
$this->request('https://yun.tim.qq.com/v5/tlssmssvr/sendVoice', $params); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function request($sendUrl, array $params) |
46
|
|
|
{ |
47
|
|
|
$params = $this->createParams($params); |
48
|
|
|
$result = $this->curl($sendUrl, json_encode($params), true); |
|
|
|
|
49
|
|
|
$this->setResult($result); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function createParams(array $params) |
53
|
|
|
{ |
54
|
|
|
$params['sig'] = $this->genSign($params); |
55
|
|
|
|
56
|
|
|
return $params; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function genSign($params) |
60
|
|
|
{ |
61
|
|
|
$phone = $params['tel']["mobile"]; |
62
|
|
|
$signature = "appkey=".$this->appKey."&random=".$this->random."&time=".$params['time']."&mobile=".$phone; |
|
|
|
|
63
|
|
|
return hash("sha256",$signature, FALSE); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
protected function setResult($result) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if ($result['request']) { |
69
|
|
|
$this->result(Agent::INFO, $result['response']); |
70
|
|
|
$result = json_decode($result['response'], true); |
71
|
|
|
if (isset($result['result'])) { |
72
|
|
|
$this->result(Agent::SUCCESS, (bool) ($result['result'] == 0)); |
73
|
|
|
$this->result(Agent::CODE, $result['result']); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$this->result(Agent::INFO, 'request failed'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getTempDataString(array $data) |
81
|
|
|
{ |
82
|
|
|
$data = array_map(function ($value) { |
83
|
|
|
return (string) $value; |
84
|
|
|
}, $data); |
85
|
|
|
|
86
|
|
|
return json_encode($data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getRandom() { |
90
|
|
|
return rand(100000, 999999); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.