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
|
|
|
private function computeSignature($parameters) |
61
|
|
|
{ |
62
|
|
|
ksort($parameters); |
63
|
|
|
$canonicalizedQueryString = ''; |
64
|
|
|
foreach ($parameters as $key => $value) { |
65
|
|
|
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); |
66
|
|
|
} |
67
|
|
|
$stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); |
68
|
|
|
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true)); |
69
|
|
|
|
70
|
|
|
return $signature; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function percentEncode($str) |
74
|
|
|
{ |
75
|
|
|
$res = urlencode($str); |
76
|
|
|
$res = preg_replace('/\+/', '%20', $res); |
77
|
|
|
$res = preg_replace('/\*/', '%2A', $res); |
78
|
|
|
$res = preg_replace('/%7E/', '~', $res); |
79
|
|
|
|
80
|
|
|
return $res; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function setResult($result) |
84
|
|
|
{ |
85
|
|
|
if ($result['request']) { |
86
|
|
|
$result = json_decode($result['response'], true); |
87
|
|
|
//dump($result); |
88
|
|
|
if (isset($result['Message'])) { |
89
|
|
|
$this->result(Agent::INFO, json_encode($result)); |
90
|
|
|
$this->result(Agent::CODE, $result['Code']); |
91
|
|
|
} else { |
92
|
|
|
$this->result(Agent::SUCCESS, true); |
93
|
|
|
$this->result(Agent::INFO, json_encode($result)); |
94
|
|
|
$this->result(Agent::CODE, 0); |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
$this->result(Agent::INFO, '请求失败'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getTempDataString(array $data) |
102
|
|
|
{ |
103
|
|
|
$data = array_map(function ($value) { |
104
|
|
|
return (string) $value; |
105
|
|
|
}, $data); |
106
|
|
|
|
107
|
|
|
return json_encode($data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function sendContentSms($to, $content) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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@property
annotation 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.