环信即时通讯-服务器端集成(REST API)-php版demo
来源:互联网 发布:联想e41指纹识别软件 编辑:程序博客网 时间:2024/09/21 06:36
原文地址:http://www.thinkphp.cn/code/1259.html
注意问题:获取APP管理员Token有次数限制,获取后需要缓存起来。
<?php
class Hxcall
{
private $app_key = 'qqqqqq#aaaaaa';
private $client_id = 'sdasdasdasd';
private $client_secret = 'sdsdsdsdsd';
private $url = "https://a1.easemob.com/qqqqqq/aaaaaa";
/*
* 获取APP管理员Token
*/
function __construct()
{
if(file_exists('./Application/Library/Token/token') && time()-filemtime('./Application/Library/Token/token')<=86400){
$this->token = file_get_contents('./Application/Library/Token/token');
}else{
$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'];
file_put_contents('./Application/Library/Token/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('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');
- 环信即时通讯-服务器端集成(REST API)-php版demo
- 环信-(php)服务器端REST API
- 环信rest服务器端集成,thinkphp实现
- 环信IM 集成服务器端(php
- 03-即时通讯 环信集成
- 环信即时通讯集成过程
- Android 环信SDK集成 即时通讯IM 一个很LOW的测试Demo
- php 百度语音识别 REST API demo
- ThinkPHP框架整合环信即时通讯DEMO
- 环信demo的集成 进阶版
- 环信即时通讯的基本集成
- Android 集成环信即时通讯修改用户密码
- Rest API学习笔记 --- 实现的 Flask 一个 RESTful API 服务器端 Demo
- 服务器端集成环信即使通讯
- 环信即时通讯Demo如何导入Studio (防丢失)
- 5分钟集成即时通讯功能——环信Android版
- 使用插件化方式快速集成环信即时通讯
- iOS开发-即时通讯第三方-环信SDK集成
- MySQL导数据
- 自定义UI(由下而上版)之第二阶段
- Decorator模式——设计模式学习笔记
- 使用C++ 调用 C# 程序
- java集合类
- 环信即时通讯-服务器端集成(REST API)-php版demo
- 手把手带你画一个动态错误提示 Android自定义view
- js正则
- TCL QUHD引行业遐想,中国有望成为显示行业绝对霸主
- GCD的顺序下载
- PhotoView的用法
- Vika and Squares
- 程序员应该关注的一些事
- ceph源码分析:Admin Socket机制