|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class AliyunSmsAgent |
|
7
|
|
|
* |
|
8
|
|
|
* @property string $accessKeyId |
|
9
|
|
|
* @property string $accessKeySecret |
|
10
|
|
|
* @property string $signName |
|
11
|
|
|
*/ |
|
12
|
|
|
class AliyunSmsAgent extends Agent |
|
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
|
|
|
'Action' => 'SingleSendSms', |
|
23
|
|
|
'SignName' => $this->signName, |
|
24
|
|
|
'ParamString' => $this->getTempDataString($data), |
|
25
|
|
|
'RecNum' => $to, |
|
26
|
|
|
'TemplateCode' => $tempId, |
|
27
|
|
|
]; |
|
28
|
|
|
$this->request($params); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
|
32
|
|
|
{ |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function request(array $params) |
|
36
|
|
|
{ |
|
37
|
|
|
$sendUrl = $this->sendUrl ?: 'https://sms.aliyuncs.com'; |
|
|
|
|
|
|
38
|
|
|
$params = $this->createParams($params); |
|
39
|
|
|
$result = $this->curl($sendUrl, $params, true); |
|
40
|
|
|
$this->setResult($result); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function createParams(array $params) |
|
44
|
|
|
{ |
|
45
|
|
|
date_default_timezone_set('GMT'); |
|
46
|
|
|
$params = array_merge([ |
|
47
|
|
|
'Format' => 'JSON', |
|
48
|
|
|
'Version' => '2016-09-27', |
|
49
|
|
|
'AccessKeyId' => $this->accessKeyId, |
|
50
|
|
|
'SignatureMethod' => 'HMAC-SHA1', |
|
51
|
|
|
'Timestamp' => date('Y-m-d\TH:i:s\Z'), |
|
52
|
|
|
'SignatureVersion' => '1.0', |
|
53
|
|
|
'SignatureNonce' => uniqid(), |
|
54
|
|
|
], $params); |
|
55
|
|
|
$params['Signature'] = $this->computeSignature($params); |
|
56
|
|
|
|
|
57
|
|
|
return $params; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
private function computeSignature($parameters) |
|
62
|
|
|
{ |
|
63
|
|
|
ksort($parameters); |
|
64
|
|
|
$canonicalizedQueryString = ''; |
|
65
|
|
|
foreach ($parameters as $key => $value) { |
|
66
|
|
|
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); |
|
67
|
|
|
} |
|
68
|
|
|
$stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); |
|
69
|
|
|
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true)); |
|
70
|
|
|
|
|
71
|
|
|
return $signature; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function percentEncode($str) |
|
75
|
|
|
{ |
|
76
|
|
|
$res = urlencode($str); |
|
77
|
|
|
$res = preg_replace('/\+/', '%20', $res); |
|
78
|
|
|
$res = preg_replace('/\*/', '%2A', $res); |
|
79
|
|
|
$res = preg_replace('/%7E/', '~', $res); |
|
80
|
|
|
|
|
81
|
|
|
return $res; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function setResult($result) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($result['request']) { |
|
87
|
|
|
$result = json_decode($result['response'], true); |
|
88
|
|
|
//dump($result); |
|
89
|
|
|
if (isset($result['Message'])) { |
|
90
|
|
|
$this->result(Agent::INFO, json_encode($result)); |
|
91
|
|
|
$this->result(Agent::CODE, $result['Code']); |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->result(Agent::SUCCESS, true); |
|
94
|
|
|
$this->result(Agent::INFO, json_encode($result)); |
|
95
|
|
|
$this->result(Agent::CODE, 0); |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
|
|
$this->result(Agent::INFO, '请求失败'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function getTempDataString(array $data) |
|
103
|
|
|
{ |
|
104
|
|
|
$data = array_map(function ($value) { |
|
105
|
|
|
return (string) $value; |
|
106
|
|
|
}, $data); |
|
107
|
|
|
|
|
108
|
|
|
return json_encode($data); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function sendContentSms($to, $content) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.