<?php
namespace Common\Controller;
use Think\Controller;
class HxController extends Controller{
/**
* 环信-服务器端REST API
*/
private $client_id = '你的应用信息';
private $client_secret = '你的应用信息';
private $org_name = '你的应用信息';
private $app_name = '你的应用信息';
private $url = '你的应用信息';
/**
* 开放注册模式
*
* @param $options['username'] 用户名
* @param $options['password'] 密码
*/
public function openRegister($options) {
$url = $this->url . "users";
$result = $this->postCurl ( $url, $options, $head = 0 );
return $result;
}

/**
* 授权注册模式 || 批量注册
*
* @param $options['username'] 用户名
* @param $options['password'] 密码
* 批量注册传二维数组
*/
public function accreditRegister($options) {
$url = $this->url . "users";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, $options, $header );
return $result;
}

/**
* 获取指定用户详情
*
* @param $username 用户名
*/
public function userDetails($username) {
$url = $this->url . "users/" . $username;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = 'GET' );
return $result;
}

/**
* 重置用户密码
*
* @param $options['username'] 用户名
* @param $options['password'] 密码
* @param $options['newpassword'] 新密码
*/
public function editPassword($options) {
$url = $this->url . "users/" . $options ['username'] . "/password";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, $options, $header, $type = 'PUT');
return $result;
}
/**
* 删除用户
*
* @param $username 用户名
*/
public function deleteUser($username) {
$url = $this->url . "users/" . $username;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = 'DELETE' );
return $result;
}

/**
* 批量删除用户
* 描述:删除某个app下指定数量的环信账号。上述url可一次删除300个用户,数值可以修改 建议这个数值在100-500之间,不要过大
*
* @param $limit="300" 默认为300条
* @param $ql 删除条件
* 如ql=order+by+created+desc 按照创建时间来排序(降序)
*/
public function batchDeleteUser($limit = "300", $ql = '') {
$url = $this->url . "users?limit=" . $limit;
if (! empty ( $ql )) {
$url = $this->url . "users?ql=" . $ql . "&limit=" . $limit;
}
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = 'DELETE' );
return $result;
}

/**
* 给一个用户添加一个好友
*
* @param
* $owner_username
* @param
* $friend_username
*/
public function addFriend($owner_username, $friend_username) {
$url = $this->url . "users/" . $owner_username . "/contacts/users/" . $friend_username;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header );
return $result;
}
/**
* 删除好友
*
* @param
* $owner_username
* @param
* $friend_username
*/
public function deleteFriend($owner_username, $friend_username) {
$url = $this->url . "users/" . $owner_username . "/contacts/users/" . $friend_username;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "DELETE" );
return $result;
}
/**
* 查看用户的好友
*
* @param
* $owner_username
*/
public function showFriend($owner_username) {
$url = $this->url . "users/" . $owner_username . "/contacts/users/";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "GET" );
return $result;
}
// +----------------------------------------------------------------------
// | 聊天相关的方法
// +----------------------------------------------------------------------
/**
* 查看用户是否在线
*
* @param
* $username
*/
public function isOnline($username) {
$url = $this->url . "users/" . $username . "/status";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "GET" );
return $result;
}
/**
* 发送消息
*
* @param string $from_user
* 发送方用户名
* @param array $username
* array('1','2')
* @param string $target_type
* 默认为:users 描述:给一个或者多个用户(users)或者群组发送消息(chatgroups)
* @param string $content
* @param array $ext
* 自定义参数
*/
function yy_hxSend($from_user = "admin", $username, $content, $target_type = "users", $ext) {
$option ['target_type'] = $target_type;
$option ['target'] = $username;
$params ['type'] = "txt";
$params ['msg'] = $content;
$option ['msg'] = $params;
$option ['from'] = $from_user;
$option ['ext'] = $ext;
$url = $this->url . "messages";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, $option, $header );
return $result;
}
/**
* 获取app中所有的群组
*/
public function chatGroups() {
$url = $this->url . "chatgroups";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "GET" );
return $result;
}
/**
* 创建群组
*
* @param $option['groupname'] //群组名称,
* 此属性为必须的
* @param $option['desc'] //群组描述,
* 此属性为必须的
* @param $option['public'] //是否是公开群,
* 此属性为必须的 true or false
* @param $option['approval'] //加入公开群是否需要批准,
* 没有这个属性的话默认是true, 此属性为可选的
* @param $option['owner'] //群组的管理员,
* 此属性为必须的
* @param $option['members'] //群组成员,此属性为可选的
*/
public function createGroups($option) {
$url = $this->url . "chatgroups";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, $option, $header );
return $result;
}
/**
* 获取群组详情
*
* @param
* $group_id
*/
public function chatGroupsDetails($group_id) {
$url = $this->url . "chatgroups/" . $group_id;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "GET" );
return $result;
}
/**
* 删除群组
*
* @param
* $group_id
*/
public function deleteGroups($group_id) {
$url = $this->url . "chatgroups/" . $group_id;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "DELETE" );
return $result;
}
/**
* 获取群组成员
*
* @param
* $group_id
*/
public function groupsUser($group_id) {
$url = $this->url . "chatgroups/" . $group_id . "/users";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "GET" );
return $result;
}
/**
* 群组添加成员
*
* @param
* $group_id
* @param
* $username
*/
public function addGroupsUser($group_id, $username) {
$url = $this->url . "chatgroups/" . $group_id . "/users/" . $username;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "POST" );
return $result;
}
/**
* 群组删除成员
*
* @param
* $group_id
* @param
* $username
*/
public function delGroupsUser($group_id, $username) {
$url = $this->url . "chatgroups/" . $group_id . "/users/" . $username;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "DELETE" );
return $result;
}
/**
* 聊天消息记录
*
* @param $ql 查询条件如:$ql
* = "select+*+where+from='" . $uid . "'+or+to='". $uid ."'+order+by+timestamp+desc&limit=" . $limit . $cursor;
* 默认为order by timestamp desc
* @param $cursor 分页参数
* 默认为空
* @param $limit 条数
* 默认20
*/
public function chatRecord($ql = '', $cursor = '', $limit = 20) {
$ql = ! empty ( $ql ) ? "ql=" . $ql : "order+by+timestamp+desc";
$cursor = ! empty ( $cursor ) ? "&cursor=" . $cursor : '';
$url = $this->url . "chatmessages?" . $ql . "&limit=" . $limit . $cursor;
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = "GET " );
return $result;
}

/**
* 获取指定用户未读消息
*
* @param $username 用户名
*/
public function userWeidu($owner_username) {
$url = $this->url . "users/" . $owner_username . "/offline_msg_count";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, '', $header, $type = 'GET' );
return $result;
}

/**
* 获取Token
*/
public function getToken() {
$option ['grant_type'] = "client_credentials";
$option ['client_id'] = $this->client_id;
$option ['client_secret'] = $this->client_secret;
$url = $this->url . "token";
$fp = @fopen ( "easemob.txt", 'r' );
if ($fp) {
$arr = unserialize ( fgets ( $fp ) );
if ($arr ['expires_in'] < time ()) {
$result = $this->postCurl ( $url, $option);
$result = explode(',', rtrim($result, '}'));
foreach ($result as $val) {
$val = explode(':', $val);
$a[] = trim($val[1], '"');
}
$result = [];
$result['access_token'] = $a[0];
$result['application'] = $a[2];
$result ['expires_in'] = $a[1] + time ();
file_put_contents("easemob.txt", serialize($result));
return $result ['access_token'];
fclose ( $fp );
exit ();
}
return $arr ['access_token'];
fclose ( $fp );
exit ();
}
$result = $this->postCurl ( $url, $option);
$result = explode(',', rtrim($result, '}'));
foreach ($result as $val) {
$val = explode(':', $val);
$a[] = trim($val[1], '"');
}
$result = [];
$result['access_token'] = $a[0];
$result['application'] = $a[2];
$result ['expires_in'] = $a[1] + time ();
file_put_contents("easemob.txt", serialize($result));
return $result ['access_token'];
fclose ( $fp );
}

/**
* CURL Post
*/
private function postCurl($url, $option, $header = 0, $type = 'POST') {
$curl = curl_init (); // 启动一个CURL会话
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
if (! empty ( $option )) {
$options = json_encode ( $option );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
}
curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header ); // 设置HTTP头
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, $type );
$result = curl_exec ( $curl ); // 执行操作
//$res = object_array ( json_decode ( $result ) );
//$res ['status'] = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
//pre ( $res );
curl_close ( $curl ); // 关闭CURL会话
return $result;
}
}

一个thinkphhp的聊天类,感觉还可以的更多相关文章

  1. 自定义一个ListView实现聊天界面

    摘要 ListView可以称得上Android中最常用也最难用的控件了,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示 ...

  2. 通过python 构建一个简单的聊天服务器

    构建一个 Python 聊天服务器 一个简单的聊天服务器 现在您已经了解了 Python 中基本的网络 API:接下来可以在一个简单的应用程序中应用这些知识了.在本节中,将构建一个简单的聊天服务器.使 ...

  3. 用C++设计一个不能被继承的类(用私有构造函数+友元函数)

    题目:用C++设计一个不能被继承的类. 分析:这是Adobe公司2007年校园招聘的最新笔试题.这道题除了考察应聘者的C++基本功底外,还能考察反应能力,是一道很好的题目. 在Java中定义了关键字f ...

  4. [SignalR]一个简单的聊天室

    原文:[SignalR]一个简单的聊天室 1.说明 开发环境:Microsoft Visual Studio 2010 以及需要安装NuGet. 2.添加SignalR所需要的类库以及脚本文件: 3. ...

  5. Cygwin 是一个用于 Windows 的类 UNIX shell 环境

    cygwin的安装使用   Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供的许多特性:以及 ...

  6. 聊天类APP功能测试总结

    做聊天类的APP 测试已经有一段时间了,也许哪天就不做了,趁今天轻松点,记录下来. 聊天类的APP都差不多,不管是微信,还是米聊,还是QQ,如果抓住了共性,测试的点都差不太多. 我用编程的术语来比方功 ...

  7. 使用ASP.NET SignalR实现一个简单的聊天室

    前言 距离我写上一篇博客已经又过了一年半载了,时间过得很快,一眨眼,就把人变得沧桑了许多.青春是短暂的,知识是无限的.要用短暂的青春,去学无穷无尽的知识,及时当勉励,岁月不待人.今天写个随笔小结记录一 ...

  8. TensorFlow练习13: 制作一个简单的聊天机器人

    现在很多卖货公司都使用聊天机器人充当客服人员,许多科技巨头也纷纷推出各自的聊天助手,如苹果Siri.Google Now.Amazon Alexa.微软小冰等等.前不久有一个视频比较了Google N ...

  9. JMS学习(四)-一个简单的聊天应用程序分析

    一,介绍 本文介绍一个简单的聊天应用程序:生产者将消息发送到Topic上,然后由ActiveMQ将该消息Push给订阅了该Topic的消费者.示例程序来自于<JAVA 消息服务--第二版 Mar ...

随机推荐

  1. 微信小程序,获取点击元素的索引值index

    1.需求说明 点击 “加号图片” 上传图片,需要知道点击的是第几个图片,动态的修改src数组,这里图片用的 wx:for 循环出来的 2.遇到问题 按照官方最新文档循环的方式,索引值是以  wx:fo ...

  2. 热词解析(9) — hangry

    今天给大家介绍一个非常有趣.又超级实用的词!!中文叫"饿极而怒",英文叫... 不知道你有没有这样的经历,当你饿着肚子等着你妈做饭,结果你妈却在麻将桌上不下来,你就越来越饿,越饿越 ...

  3. 统计nginx日志里每五分钟的访问量

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Random_lee import time import os import re cla ...

  4. 第十五节:Web爬虫之selenium动态渲染爬取

    selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firef ...

  5. PAT 1125 Chain the Ropes

    Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...

  6. 2.5.4 华丽的 printf 输出

        如同echo命令,printf命令可以输出简单的字符串:         [many@avention my_sh]$ printf "Hello, world\n"    ...

  7. Linux学习总结(21)——CentOS7环境下FTP服务器的安装和配置

    1. 安装vsftpd #安装vsftpd yum install -y vsftpd #设置开机启动 systemctl enable vsftpd.service # 重启 service vsf ...

  8. ansible roles例子

    #理解 changed_when failed_when become become_user ansible_become ansible_become_user static #检查group_v ...

  9. Android layer-list:联合shape(2)

     Android layer-list:联合shape(2) 附录文章3简单说明了Android layer-list的用法,现在把Android layer-list联合shape做出一些特殊的 ...

  10. noip模拟赛 运

    [问题背景]zhx 和妹子们玩数数游戏.[问题描述]仅包含 4 或 7 的数被称为幸运数.一个序列的子序列被定义为从序列中删去若干个数, 剩下的数组成的新序列.两个子序列被定义为不同的当且仅当其中的元 ...