首页 技术 正文
技术 2022年11月21日
0 收藏 846 点赞 2,716 浏览 4191 个字

<?php

class Hxcall
{

private $app_key = ‘yunjiankang#medical’;

private $client_id = ‘YXA6ARjBgDnxEeabYgu2ntsuFw’;

private $client_secret = ‘YXA6bW6DcFhnjwgAobVekEXRWqvWTb4’;

private $url = “https://a1.easemob.com/yunjiankang/medical”;
/*
* 获取APP管理员Token
*/
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 Hxcall();
// 注册的用户
//echo $rs->hx_register(‘qwerasd’, ‘qazwsx’, ‘福州123’ );
// 给IM用户的添加好友
// echo $rs->hx_contacts(‘test2’, ‘test1’);
/* 发送文本消息 */
// echo $rs->hx_send(‘qwerasd’,’test3′,’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(‘qwerasd’);
/* 修改用户昵称 */
// 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’);

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,001
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850