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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
<?php
namespace hx;
class Hx
{
private $app_key = '1106196338107043#bfzb';
private $client_id = 'YXA68gwqIDr4Eem08DO8sr_xCA';
private $client_secret = 'YXA57if_NzYCtL_96DktWsvILrlcU24';
private $url = "https://a1.easemob.com/1106190228107043/bfzb";
/*
* 获取APP管理员Token
*/
public function __construct()
{
$url = $this->url . "/token";
$data = array(
'grant_type' => 'client_credentials',
'client_id' => $this->client_id,
'client_secret' => $this->client_secret
);
$rs = json_decode($this->curl($url, $data), true);
$this->token = $rs['access_token'];
}
/*
* 注册IM用户(授权注册)
*/
public function hx_register($username, $password, $nickname)
{
$url = $this->url . "/users";
$data = array(
'username' => $username,
'password' => $password,
'nickname' => $nickname
);
$header = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, $data, $header, "POST");
}
/*
* 给IM用户的添加好友
*/
public function hx_contacts($owner_username, $friend_username)
{
$url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
$header = array(
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, "", $header, "POST");
}
/*
* 解除IM用户的好友关系
*/
public function hx_contacts_delete($owner_username, $friend_username)
{
$url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
$header = array(
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, "", $header, "DELETE");
}
/*
* 查看好友
*/
public function hx_contacts_user($owner_username)
{
$url = $this->url . "/users/${owner_username}/contacts/users";
$header = array(
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, "", $header, "GET");
}
/* 发送文本消息 */
public function hx_send($sender, $receiver, $msg)
{
$url = $this->url . "/messages";
$header = array(
'Authorization: Bearer ' . $this->token
);
$data = array(
'target_type' => 'users',
'target' => array(
'0' => $receiver
),
'msg' => array(
'type' => "txt",
'msg' => $msg
),
'from' => $sender,
'ext' => array(
'attr1' => 'v1',
'attr2' => "v2"
)
);
return $this->curl($url, $data, $header, "POST");
}
/* 查询离线消息数 获取一个IM用户的离线消息数 */
public function hx_msg_count($owner_username)
{
$url = $this->url . "/users/${owner_username}/offline_msg_count";
$header = array(
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, "", $header, "GET");
}
/*
* 获取IM用户[单个]
*/
public function hx_user_info($username)
{
$url = $this->url . "/users/${username}";
$header = array(
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, "", $header, "GET");
}
/*
* 获取IM用户[批量]
*/
public function hx_user_infos($limit)
{
$url = $this->url . "/users?${limit}";
$header = array(
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, "", $header, "GET");
}
/*
* 重置IM用户密码
*/
public function hx_user_update_password($username, $newpassword)
{
$url = $this->url . "/users/${username}/password";
$header = array(
'Authorization: Bearer ' . $this->token
);
$data['newpassword'] = $newpassword;
return $this->curl($url, $data, $header, "PUT");
}
/*
* 删除IM用户[单个]
*/
public function hx_user_delete($username)
{
$url = $this->url . "/users/${username}";
$header = array(
'Authorization: Bearer ' . $this->token
);
return $this->curl($url, "", $header, "DELETE");
}
/*
* 修改用户昵称
*/
public function hx_user_update_nickname($username, $nickname)
{
$url = $this->url . "/users/${username}";
$header = array(
'Authorization: Bearer ' . $this->token
);
$data['nickname'] = $nickname;
return $this->curl($url, $data, $header, "PUT");
}
/*
*
* curl
*/
private function curl($url, $data, $header = false, $method = "POST")
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$ret = curl_exec($ch);
return $ret;
}
}
$rs = new Hx();
// 注册的用户
echo $rs->hx_register('qwerasd', 'qazwsx', '福州123');
// 给IM用户的添加好友
// echo $rs->hx_contacts('admin888', 'qwerasd');
/* 发送文本消息 */
// echo $rs->hx_send('213123','admin888','dfadsr214wefaedf');
/* 消息数统计 */
// echo $rs->hx_msg_count('admin888');
/* 获取IM用户[单个] */
// echo $rs->hx_user_info('admin888');
/* 获取IM用户[批量] */
// echo $rs->hx_user_infos('20');
/* 删除IM用户[单个] */
// echo $rs->hx_user_delete('wwwwww');
/* 修改用户昵称 */
// echo $rs->hx_user_update_nickname('asaxcfasdd','网络科技');
/* 重置IM用户密码 */
// echo $rs->hx_user_update_password('asaxcfasdd','asdad');
/* 解除IM用户的好友关系 */
// echo $rs->hx_contacts_delete('admin888', 'qqqqqqqq');
/* 查看好友 */
//echo $rs->hx_contacts_user('admin888');
|