环信即时通讯---php版
2020-12-16 22:11
标签:ssl his omr orm tar class receiver cat Owner 环信即时通讯---php版 标签:ssl his omr orm tar class receiver cat Owner 原文地址:https://www.cnblogs.com/qichao123/p/14115084.html 1 php
2 /**
3 * 环信IM
4 * Created by PhpStorm.
5 * User: chao
6 * Date: 2020/12/9
7 * Time: 4:34 PM
8 */
9 10
11 class HxChat{
12 private $app_key = ‘123456#abcd‘;
13 private $client_id = ‘hhkhkkkhh‘;
14 private $client_secret = ‘uiuriwrinncj‘;
15 private $url = "https://a1.easemob.com/123456/abcd"; //要与$app_key值保持一致
16
17 /*
18 * 获取APP管理员Token
19 */
20 function __construct()
21 {
22 $url = $this->url . "/token";
23 $data = array(
24 ‘grant_type‘ => ‘client_credentials‘,
25 ‘client_id‘ => $this->client_id,
26 ‘client_secret‘ => $this->client_secret
27 );
28 $rs = json_decode($this->curl($url, $data), true);
29 $this->token = $rs[‘access_token‘];
30 }
31
32 /**
33 * 注册单个用户(开放)
34 * @param $username
35 * @param $password
36 * @param string $nickname
37 */
38 public function hx_open_register($username, $password, $nickname=‘‘)
39 {
40 $url = $this->url . "/users";
41 $data = array(
42 ‘username‘=>$username,
43 ‘password‘=>$password,
44 ‘nickname‘=>$nickname,
45 );
46 $header = array(
47 ‘Content-Type: application/json‘,
48 );
49
50 return $this->curl($url, $data, $header, ‘POST‘);
51 }
52
53 /**
54 * 注册单个用户(授权)
55 * @param $username
56 * @param $password
57 * @param string $nickname
58 */
59 public function hx_auth_register($username, $password, $nickname=‘‘)
60 {
61 $url = $this->url . "/users";
62 $data = array(
63 ‘username‘=>$username,
64 ‘password‘=>$password,
65 ‘nickname‘=>$nickname,
66 );
67 $header = array(
68 ‘Content-Type: application/json‘,
69 ‘Authorization: Bearer ‘.$this->token,
70 );
71 return $this->curl($url, $data, $header, ‘POST‘);
72
73 }
74
75 /**
76 * 添加好友
77 * @param $owner_username 要添加好友的用户名
78 * @param $friend_username 好友用户名
79 * @return mixed
80 */
81 public function hx_add_friend($owner_username, $friend_username)
82 {
83 $url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
84 $header = array(
85 ‘Content-Type: application/json‘,
86 ‘Authorization: Bearer ‘.$this->token,
87 );
88
89 return $this->curl($url, ‘‘ , $header, ‘POST‘);
90 }
91
92 /**
93 * 移除好友
94 * @param $owner_username
95 * @param $friend_username
96 * @return mixed
97 */
98 public function hx_contacts_delete($owner_username, $friend_username)
99 {
100 $url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
101 $header = array(
102 ‘Authorization: Bearer ‘ . $this->token
103 );
104 return $this->curl($url, "", $header, "DELETE");
105 }
106
107 /**
108 * 获取好友列表
109 * @param $owner_username
110 * @return mixed
111 */
112 public function hx_contacts_user($owner_username)
113 {
114 $url = $this->url . "/users/${owner_username}/contacts/users";
115 $header = array(
116 ‘Authorization: Bearer ‘ . $this->token
117 );
118 return $this->curl($url, "", $header, "GET");
119 }
120
121 /**
122 * 发送文本消息
123 * @param $sender 发送者
124 * @param $receiver 接收者
125 * @param $msg 消息
126 * @return mixed
127 */
128 public function hx_send_txt_msg($sender, $receiver, $msg)
129 {
130 $url = $this->url . "/messages";
131 $header = array(
132 ‘Authorization: Bearer ‘ . $this->token,
133 ‘Content-Type: application/json‘
134 );
135 $data = array(
136 ‘target_type‘=> ‘users‘,
137 ‘target‘=>[$receiver],
138 ‘msg‘=>[‘msg‘=>$msg, ‘type‘=>‘txt‘],
139 ‘from‘=>$sender,
140 );
141 return $this->curl($url, $data, $header, "POST");
142 }
143
144 /**
145 * curl
146 * @param $url
147 * @param $data
148 * @param bool $header
149 * @param string $method
150 * @return mixed
151 */
152 private function curl($url, $data, $header = false, $method = "POST")
153 {
154 $ch = curl_init($url);
155 curl_setopt($ch, CURLOPT_URL, $url);
156 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
157 if ($header) {
158 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
159 }
160 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
161 if ($data) {
162 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
163 }
164 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
165 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
166 $ret = curl_exec($ch);
167 return $ret;
168 }
169 }
上一篇:jquery的树状菜单
下一篇:django 上传和下载文件