TokenGetterForAlicom.php
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
use Aliyun\Api\Msg\Request\V20170525\QueryTokenForMnsQueueRequest;
use Aliyun\Core\Config;
use AliyunMNS\Client;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Core\Exception\ClientException;
use Aliyun\Core\Exception\ServerException;
// 加载区域结点配置
Config::load();
/**
* 接收云通信消息的临时token
*
* @property array tokenMap
* @property int bufferTime 过期时间小于2分钟则重新获取,防止服务器时间误差
* @property string mnsAccountEndpoint
* @property \Aliyun\Core\DefaultAcsClient acsClient
*/
class TokenGetterForAlicom
{
/**
* TokenGetterForAlicom constructor.
*
* @param string $accountId AccountId
* @param string $accessKeyId AccessKeyId
* @param string $accessKeySecret AccessKeySecret
*/
public function __construct($accountId, $accessKeyId, $accessKeySecret)
{
$endpointName = "cn-hangzhou";
$regionId = "cn-hangzhou";
$productName = "Dybaseapi";
$domain = "dybaseapi.aliyuncs.com";
$this->tokenMap = [];
$this->bufferTime = 2 * 60;
DefaultProfile::addEndpoint($endpointName, $regionId, $productName, $domain);
$profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
$this->acsClient = new DefaultAcsClient($profile);
$this->mnsAccountEndpoint = $this->getAccountEndpoint($accountId, $regionId);
}
/**
* 配置获取AccountEndPoint
*
* @param string $accountId AccountId
* @param string $region Region
* @param bool $secure 是否启用https
* @param bool $internal
* @param bool $vpc
* @return string <ul>
* <li>http(s)://{AccountId}.mns.cn-beijing.aliyuncs.com</li>
* <li>http(s)://{AccountId}.mns.cn-beijing-internal.aliyuncs.com</li>
* <li>http(s)://{AccountId}.mns.cn-beijing-internal-vpc.aliyuncs.com</li>
* </ul>
*/
private function getAccountEndpoint($accountId, $region, $secure=false, $internal=false, $vpc=false)
{
$protocol = $secure ? 'https' : 'http';
$realRegion = $region;
if ($internal) {
$realRegion .= '-internal';
}
if ($vpc) {
$realRegion .= '-vpc';
}
return "$protocol://$accountId.mns.$realRegion.aliyuncs.com";
}
/**
* 远程取Token
*
* @param string $messageType 消息订阅类型 SmsReport | SmsUp
* @return TokenForAlicom|bool
*/
public function getTokenFromRemote($messageType, $queueName)
{
$request = new QueryTokenForMnsQueueRequest();
$request->setMessageType($messageType);
$request->setQueueName($queueName);
try {
$response = $this->acsClient->getAcsResponse($request);
// print_r($response);
$tokenForAlicom = new TokenForAlicom();
$tokenForAlicom->setMessageType($messageType);
$tokenForAlicom->setToken($response->MessageTokenDTO->SecurityToken);
$tokenForAlicom->setTempAccessKey($response->MessageTokenDTO->AccessKeyId);
$tokenForAlicom->setTempSecret($response->MessageTokenDTO->AccessKeySecret);
$tokenForAlicom->setExpireTime($response->MessageTokenDTO->ExpireTime);
// print_r($tokenForAlicom);
return $tokenForAlicom;
}
catch (ServerException $e) {
print_r($e->getErrorCode());
print_r($e->getErrorMessage());
}
catch (ClientException $e) {
print_r($e->getErrorCode());
print_r($e->getErrorMessage());
}
return false;
}
/**
* 先从tokenMap中取,如果不存在则远程取Token并存入tokenMap
*
* @param string $messageType 消息订阅类型 SmsReport | SmsUp
* @param string $queueName 在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName<br/>(e.g. Alicom-Queue-xxxxxx-SmsReport)
* @return TokenForAlicom|bool
*/
public function getTokenByMessageType($messageType, $queueName)
{
$tokenForAlicom = null;
if(isset($this->tokenMap[$messageType])) {
$tokenForAlicom = $this->tokenMap[$messageType];
}
if(null == $tokenForAlicom || strtotime($tokenForAlicom->getExpireTime()) - time() > $this->bufferTime)
{
$tokenForAlicom =$this->getTokenFromRemote($messageType, $queueName);
$client = new Client(
$this->mnsAccountEndpoint,
$tokenForAlicom->getTempAccessKey(),
$tokenForAlicom->getTempSecret(),
$tokenForAlicom->getToken()
);
$tokenForAlicom->setClient($client);
$tokenForAlicom->setQueue($queueName);
$this->tokenMap[$messageType] = $tokenForAlicom;
}
return $tokenForAlicom;
}
}