1 <?php2 /**3 * 环信IM4 * Created by PhpStorm.5 * User: chao6 * Date: 2020/12/97 * Time: 4:34 PM8 */
910
11 classHxChat{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管理员Token19 */
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_secret27 );28 $rs = json_decode($this->curl($url, $data), true);29 $this->token = $rs['access_token'];30 }31
32 /**33 * 注册单个用户(开放)34 * @param $username35 * @param $password36 * @param string $nickname37 */
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 $username56 * @param $password57 * @param string $nickname58 */
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 mixed80 */
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_username95 * @param $friend_username96 * @return mixed97 */
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->token103 );104 return $this->curl($url, "", $header, "DELETE");105 }106
107 /**108 * 获取好友列表109 * @param $owner_username110 * @return mixed111 */
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->token117 );118 return $this->curl($url, "", $header, "GET");119 }120
121 /**122 * 发送文本消息123 * @param $sender 发送者124 * @param $receiver 接收者125 * @param $msg 消息126 * @return mixed127 */
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 * curl146 * @param $url147 * @param $data148 * @param bool $header149 * @param string $method150 * @return mixed151 */
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 }