Account.php
5.58 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace App\Common\TencentMQ;
/*
Account 对象非线程安全,如果多线程使用,需要每个线程单独初始化Account对象类
*/
class Account
{
private $secretId;
private $secretKey;
private $cmq_client;
/*
@type host: string
@param host: 访问的url,例如:https://cmq-queue-gz.api.qcloud.com
@type secretId: string
@param secretId: 用户的secretId, 腾讯云官网获取
@type secretKey: string
@param secretKey: 用户的secretKey,腾讯云官网获取
@note: Exception
:: CMQClientParameterException host格式错误
*/
public function __construct($host, $secretId, $secretKey) {
$this->host = $host;
$this->secretId = $secretId;
$this->secretKey = $secretKey;
$this->cmq_client = new CMQClient($host, $secretId, $secretKey);
}
/*
* @type sign_method:string
* @param sign_method : only support sha1 and sha256
*/
public function set_sign_method($sign_method='sha1')
{
$this->cmq_client->set_sign_method($sign_method);
}
/* 设置访问的url
@type host: string
@param host: 访问的url,例如:http://cmq-queue-gz.api.tencentyun.com
@type secretId: string
@param secretId: 用户的secretId,腾讯云官网获取
@type secretKey: string
@param secretKey: 用户的secretKey,腾讯云官网获取
@note: Exception
:: CMQClientParameterException host格式错误
*/
public function set_client($host, $secretId=NULL, $secretKey=NULL) {
if ($secretId == NULL) {
$secretId = $this->secretId;
}
if ($secretKey == NULL) {
$secretKey = $this->secretKey;
}
$this->cmq_client = new CMQClient($host, $secretId, $secretKey);
}
/* 获取queue client
@rtype: CMQClient object
@return: 返回使用的CMQClient object
*/
public function get_client() {
return $this->cmq_client;
}
/* 获取Account的一个Queue对象
@type queue_name: string
@param queue_name: 队列名
@rtype: Queue object
@return: 返回该Account的一个Queue对象
*/
public function get_queue($queue_name) {
return new Queue($queue_name, $this->cmq_client);
}
/* 列出Account的队列
@type searchWord: string
@param searchWord: 队列名的前缀
@type limit: int
@param limit: list_queue最多返回的队列数
@type offset: string
@param offset: list_queue的起始位置,上次list_queue返回的next_offset
@rtype: tuple
@return: QueueURL的列表和下次list queue的起始位置; 如果所有queue都list出来,next_offset为"".
*/
public function list_queue($searchWord = "", $limit = -1, $offset = "") {
$params = array();
if ($searchWord != "") {
$params['searchWord'] = $searchWord;
}
if ($limit != -1) {
$params['limit'] = $limit;
}
if ($offset != "") {
$params['offset'] = $offset;
}
$ret_pkg = $this->cmq_client->list_queue($params);
if ($offset == "") {
$next_offset = count($ret_pkg['queueList']);
}
else {
$next_offset = $offset + count($ret_pkg['queueList']);
}
if ($next_offset >= $ret_pkg['totalCount']) {
$next_offset = "";
}
return array("totalCount"=>$ret_pkg['totalCount'],
"queueList"=>$ret_pkg['queueList'], "next_offset"=>$next_offset);
}
/* 列出Account的主题
@type searchWord: string
@param searchWord: 主题关键字
@type limit: int
@param limit: 最多返回的主题数目
@type offset: string
@param offset: list_topic的起始位置,上次list_topic返回的next_offset
@rtype: tuple
@return: TopicURL的列表和下次list topic的起始位置; 如果所有topic都list出来,next_offset为"".
*/
public function list_topic($searchWord = "", $limit = -1, $offset = ""){
$params = array();
if($searchWord != ""){
$params['searchWord'] = $searchWord;
}
if($limit != -1){
$params['limit'] = $limit;
}
if($offset != ""){
$params['offset'] = $offset;
}
$resp = $this->cmq_client->list_topic($params);
if ($offset == ""){
$next_offset = count($resp['topicList']);
}
else{
$next_offset = $offset + count($resp['topicList']);
}
if($next_offset >= $resp['totalCount']){
$next_offset = "";
}
return array("totalCoult" => $resp['totalCount'],
"topicList" =>$resp['topicList'],
"next_offset" => $next_offset);
}
/* 获取Account的一个Topic对象
@type topic_name: string
@param queue_name:
@rtype: Topic object
@return: 返回该Account的一个Topic对象
*/
public function get_topic($topic_name)
{
return new Topic($topic_name,$this->cmq_client);
}
/* 获取Account的一个Subscription对象
@type topic_name: string
@param queue_name:
@type subscription_name :string
@param subscription_name:
@rtype: Subscription object
@return: 返回该Account的一个Subscription对象
*/
public function get_subscription($topic_name, $subscription_name)
{
return new Subscription($topic_name, $subscription_name,$this->cmq_client);
}
}