1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class SendCloudAgent |
7
|
|
|
* |
8
|
|
|
* @property string $smsUser |
9
|
|
|
* @property string $smsKey |
10
|
|
|
*/ |
11
|
|
|
class SendCloudAgent extends Agent implements TemplateSms, VoiceCode |
12
|
|
|
{ |
13
|
|
|
public function sendTemplateSms($to, $tempId, array $data) |
14
|
|
|
{ |
15
|
|
|
$params = [ |
16
|
|
|
'msgType' => 0, |
17
|
|
|
'vars' => $this->getTempDataString($data), |
18
|
|
|
'phone' => $to, |
19
|
|
|
'templateId' => $tempId, |
20
|
|
|
]; |
21
|
|
|
$this->request('http://sendcloud.sohu.com/smsapi/send', $params); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function sendVoiceCode($to, $code) |
25
|
|
|
{ |
26
|
|
|
$params = [ |
27
|
|
|
'phone' => $to, |
28
|
|
|
'code' => $code, |
29
|
|
|
]; |
30
|
|
|
$this->request('http://sendcloud.sohu.com/smsapi/sendVoice', $params); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function request($sendUrl, array $params) |
34
|
|
|
{ |
35
|
|
|
$params['smsUser'] = $this->smsUser; |
36
|
|
|
$params['signature'] = $this->genSign($params); |
37
|
|
|
$result = $this->curlPost($sendUrl, $params); |
38
|
|
|
$this->setResult($result); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function genSign($params) |
42
|
|
|
{ |
43
|
|
|
ksort($params); |
44
|
|
|
$stringToBeSigned = ''; |
45
|
|
|
foreach ($params as $k => $v) { |
46
|
|
|
$stringToBeSigned .= $k . '=' . $v . '&'; |
47
|
|
|
} |
48
|
|
|
$stringToBeSigned = trim($stringToBeSigned, '&'); |
49
|
|
|
$stringToBeSigned = $this->smsKey . '&' . $stringToBeSigned . '&' . $this->smsKey; |
50
|
|
|
|
51
|
|
|
return md5($stringToBeSigned); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
protected function setResult($result) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if ($result['request']) { |
57
|
|
|
$this->result(Agent::INFO, $result['response']); |
58
|
|
|
$result = json_decode($result['response'], true); |
59
|
|
|
if (isset($result['result'])) { |
60
|
|
|
$this->result(Agent::SUCCESS, (bool) $result['result']); |
61
|
|
|
$this->result(Agent::CODE, $result['statusCode']); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
$this->result(Agent::INFO, 'request failed'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function getTempDataString(array $data) |
69
|
|
|
{ |
70
|
|
|
return json_encode(array_map('strval', $data)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.