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 |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public function sendSms($to, $content, $tempId, array $data) |
15
|
|
|
{ |
16
|
|
|
$this->sendTemplateSms($to, $tempId, $data); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function sendTemplateSms($to, $tempId, array $data) |
20
|
|
|
{ |
21
|
|
|
$params = [ |
22
|
|
|
'msgType' => 0, |
23
|
|
|
'vars' => $this->getTempDataString($data), |
24
|
|
|
'phone' => $to, |
25
|
|
|
'templateId' => $tempId, |
26
|
|
|
]; |
27
|
|
|
$this->request('http://sendcloud.sohu.com/smsapi/send', $params); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
31
|
|
|
{ |
32
|
|
|
$params = [ |
33
|
|
|
'phone' => $to, |
34
|
|
|
'code' => $code, |
35
|
|
|
]; |
36
|
|
|
$this->request('http://sendcloud.sohu.com/smsapi/sendVoice', $params); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function request($sendUrl, array $params) |
40
|
|
|
{ |
41
|
|
|
$params = $this->createParams($params); |
42
|
|
|
$result = $this->curl($sendUrl, $params, true); |
43
|
|
|
$this->setResult($result); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function createParams(array $params) |
47
|
|
|
{ |
48
|
|
|
$params = array_merge([ |
49
|
|
|
'smsUser' => $this->smsUser, |
50
|
|
|
], $params); |
51
|
|
|
$params['signature'] = $this->genSign($params); |
52
|
|
|
|
53
|
|
|
return $params; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function genSign($params) |
57
|
|
|
{ |
58
|
|
|
ksort($params); |
59
|
|
|
$stringToBeSigned = ''; |
60
|
|
|
foreach ($params as $k => $v) { |
61
|
|
|
$stringToBeSigned .= $k . '=' . $v . '&'; |
62
|
|
|
} |
63
|
|
|
$stringToBeSigned = trim($stringToBeSigned, '&'); |
64
|
|
|
$stringToBeSigned = $this->smsKey . '&' . $stringToBeSigned . '&' . $this->smsKey; |
65
|
|
|
|
66
|
|
|
return md5($stringToBeSigned); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function setResult($result) |
70
|
|
|
{ |
71
|
|
|
var_dump($result); |
|
|
|
|
72
|
|
|
if ($result) { |
73
|
|
|
if (isset($result['result'])) { |
74
|
|
|
$this->result(Agent::SUCCESS, (bool) $result['result']); |
75
|
|
|
$this->result(Agent::INFO, $error['message']); |
|
|
|
|
76
|
|
|
$this->result(Agent::CODE, $result['statusCode']); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
$this->result(Agent::INFO, '请求失败'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function getTempDataString(array $data) |
84
|
|
|
{ |
85
|
|
|
return json_encode(array_map('strval', $data)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function sendContentSms($to, $content) |
89
|
|
|
{ |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|