|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Toplan\PhpSms\Agent; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ChangZhuoAgent |
|
10
|
|
|
* |
|
11
|
|
|
* @property string $account |
|
12
|
|
|
*/ |
|
13
|
|
|
class AliyuncsAgent extends Agent |
|
14
|
|
|
{ |
|
15
|
|
|
public function sendSms($to, $content, $tempData, array $data) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->sendTemplateSms($to, $tempData, $data); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function sendTemplateSms($to, $tempData, array $data) |
|
21
|
|
|
{ |
|
22
|
|
|
$params = [ |
|
23
|
|
|
// 公共参数 |
|
24
|
|
|
'Format' => $this->format, |
|
|
|
|
|
|
25
|
|
|
'Version' => $this->version, |
|
|
|
|
|
|
26
|
|
|
'AccessKeyId' => $this->accessKeyID, |
|
|
|
|
|
|
27
|
|
|
'SignatureVersion' => $this->signatureVersion, |
|
|
|
|
|
|
28
|
|
|
'SignatureMethod' => $this->signatureMethod, |
|
|
|
|
|
|
29
|
|
|
'SignatureNonce'=> uniqid(), |
|
30
|
|
|
'Timestamp' => Carbon::now('UTC')->format('Y-m-d\TH:i:s\Z'), |
|
31
|
|
|
'RegionId' => 'cn-hangzhou', |
|
32
|
|
|
// 接口参数 |
|
33
|
|
|
'Action' => 'SingleSendSms', |
|
34
|
|
|
'SignName' => $tempData['signName'], |
|
35
|
|
|
'ParamString' => $this->getTempDataString($data), |
|
36
|
|
|
'RecNum' => $this->recNum($to), |
|
37
|
|
|
'TemplateCode' => $tempData['templateCode'], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
$url = 'https://sms.aliyuncs.com/'; |
|
41
|
|
|
$params['Signature'] = $this->computeSignature($params, $this->accessKeySecret); |
|
|
|
|
|
|
42
|
|
|
$params = http_build_query($params); |
|
43
|
|
|
$response = $this->sockPost($url, $params); |
|
|
|
|
|
|
44
|
|
|
$this->setResult($response); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function getTempDataString(array $data) |
|
48
|
|
|
{ |
|
49
|
|
|
$data = array_map(function ($value) { |
|
50
|
|
|
return (string) $value; |
|
51
|
|
|
}, $data); |
|
52
|
|
|
|
|
53
|
|
|
return json_encode($data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function sendContentSms($to, $content) |
|
57
|
|
|
{ |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
|
61
|
|
|
{ |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function percentEncode($str) |
|
65
|
|
|
{ |
|
66
|
|
|
// 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范 |
|
|
|
|
|
|
67
|
|
|
$res = urlencode($str); |
|
68
|
|
|
$res = preg_replace('/\+/', '%20', $res); |
|
69
|
|
|
$res = preg_replace('/\*/', '%2A', $res); |
|
70
|
|
|
$res = preg_replace('/%7E/', '~', $res); |
|
71
|
|
|
return $res; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function computeSignature($parameters, $accessKeySecret) |
|
75
|
|
|
{ |
|
76
|
|
|
// 将参数Key按字典顺序排序 |
|
77
|
|
|
ksort($parameters); |
|
78
|
|
|
// 生成规范化请求字符串 |
|
79
|
|
|
$canonicalizedQueryString = ''; |
|
80
|
|
|
foreach ($parameters as $key => $value) { |
|
81
|
|
|
$canonicalizedQueryString .= '&' . $this->percentEncode($key) |
|
82
|
|
|
. '=' . $this->percentEncode($value); |
|
83
|
|
|
} |
|
84
|
|
|
// 生成用于计算签名的字符串 stringToSign |
|
85
|
|
|
$stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); |
|
86
|
|
|
// 计算签名,注意accessKeySecret后面要加上字符'&' |
|
87
|
|
|
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true)); |
|
88
|
|
|
return $signature; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function recNum($data) |
|
92
|
|
|
{ |
|
93
|
|
|
if (is_array($data)) { |
|
94
|
|
|
$data = implode(',', $data); |
|
95
|
|
|
} |
|
96
|
|
|
return $data; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function setResult($result) |
|
100
|
|
|
{ |
|
101
|
|
|
$result =json_decode($result, true); |
|
102
|
|
|
if (isset($result['Message'])) { |
|
103
|
|
|
$this->result(Agent::INFO, '请求失败'); |
|
104
|
|
|
} else { |
|
105
|
|
|
$this->result(Agent::INFO, $result); |
|
106
|
|
|
$this->result(Agent::SUCCESS, (bool) $result); |
|
107
|
|
|
$this->result(Agent::CODE, 0); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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.