打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
环信即时通讯

环信即时通讯

浏览:3793 发布日期:2015/06/04 分类:业务逻辑 关键字: 即时通讯
环信即时通讯
地址:http://www.easemob.com/ 可以做一个聊天系统
根据接口文档 我整理了下!分享出来
  1. <?php

  2. class Hxcall
  3. {
  4.     

  5.     private $app_key = 'qqqqqq#aaaaaa';

  6.     private $client_id = 'sdasdasdasd';

  7.     private $client_secret = 'sdsdsdsdsd';

  8.     private $url = "https://a1.easemob.com/qqqqqq/aaaaaa";
  9.     /*
  10.      * 获取APP管理员Token
  11.      */
  12.     function __construct()
  13.     {
  14.         $url = $this->url . "/token";
  15.         $data = array(
  16.             'grant_type' => 'client_credentials',
  17.             'client_id' => $this->client_id,
  18.             'client_secret' => $this->client_secret
  19.         );
  20.         $rs = json_decode($this->curl($url, $data), true);
  21.         $this->token = $rs['access_token'];
  22.     }
  23.     /*
  24.      * 注册IM用户(授权注册)
  25.      */
  26.     public function hx_register($username, $password, $nickname)
  27.     {
  28.         $url = $this->url . "/users";
  29.         $data = array(
  30.             'username' => $username,
  31.             'password' => $password,
  32.             'nickname' => $nickname
  33.         );
  34.         $header = array(
  35.             'Content-Type: application/json',
  36.             'Authorization: Bearer ' . $this->token
  37.         );
  38.         return $this->curl($url, $data, $header, "POST");
  39.     }
  40.     /*
  41.      * 给IM用户的添加好友
  42.      */
  43.     public function hx_contacts($owner_username, $friend_username)
  44.     {
  45.         $url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
  46.         $header = array(
  47.             'Authorization: Bearer ' . $this->token
  48.         );
  49.         return $this->curl($url, "", $header, "POST");
  50.     }
  51.     /*
  52.      * 解除IM用户的好友关系
  53.      */
  54.     public function hx_contacts_delete($owner_username, $friend_username)
  55.     {
  56.         $url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
  57.         $header = array(
  58.             'Authorization: Bearer ' . $this->token
  59.         );
  60.         return $this->curl($url, "", $header, "DELETE");
  61.     }
  62.     /*
  63.      * 查看好友
  64.      */
  65.     public function hx_contacts_user($owner_username)
  66.     {
  67.         $url = $this->url . "/users/${owner_username}/contacts/users";
  68.         $header = array(
  69.             'Authorization: Bearer ' . $this->token
  70.         );
  71.         return $this->curl($url, "", $header, "GET");
  72.     }
  73.     
  74.     /* 发送文本消息 */
  75.     public function hx_send($sender, $receiver, $msg)
  76.     {
  77.         $url = $this->url . "/messages";
  78.         $header = array(
  79.             'Authorization: Bearer ' . $this->token
  80.         );
  81.         $data = array(
  82.             'target_type' => 'users',
  83.             'target' => array(
  84.                 '0' => $receiver
  85.             ),
  86.             'msg' => array(
  87.                 'type' => "txt",
  88.                 'msg' => $msg
  89.             ),
  90.             'from' => $sender,
  91.             'ext' => array(
  92.                 'attr1' => 'v1',
  93.                 'attr2' => "v2"
  94.             )
  95.         );
  96.         return $this->curl($url, $data, $header, "POST");
  97.     }
  98.     /* 查询离线消息数 获取一个IM用户的离线消息数 */
  99.     public function hx_msg_count($owner_username)
  100.     {
  101.         $url = $this->url . "/users/${owner_username}/offline_msg_count";
  102.         $header = array(
  103.             'Authorization: Bearer ' . $this->token
  104.         );
  105.         return $this->curl($url, "", $header, "GET");
  106.     }
  107.     
  108.     /*
  109.      * 获取IM用户[单个]
  110.      */
  111.     public function hx_user_info($username)
  112.     {
  113.         $url = $this->url . "/users/${username}";
  114.         $header = array(
  115.             'Authorization: Bearer ' . $this->token
  116.         );
  117.         return $this->curl($url, "", $header, "GET");
  118.     }
  119.     /*
  120.      * 获取IM用户[批量]
  121.      */
  122.     public function hx_user_infos($limit)
  123.     {
  124.         $url = $this->url . "/users?${limit}";
  125.         $header = array(
  126.             'Authorization: Bearer ' . $this->token
  127.         );
  128.         return $this->curl($url, "", $header, "GET");
  129.     }
  130.     /*
  131.      * 重置IM用户密码
  132.      */
  133.     public function hx_user_update_password($username, $newpassword)
  134.     {
  135.         $url = $this->url . "/users/${username}/password";
  136.         $header = array(
  137.             'Authorization: Bearer ' . $this->token
  138.         );
  139.         $data['newpassword'] = $newpassword;
  140.         return $this->curl($url, $data, $header, "PUT");
  141.     }
  142.     
  143.     /*
  144.      * 删除IM用户[单个]
  145.      */
  146.     public function hx_user_delete($username)
  147.     {
  148.         $url = $this->url . "/users/${username}";
  149.         $header = array(
  150.             'Authorization: Bearer ' . $this->token
  151.         );
  152.         return $this->curl($url, "", $header, "DELETE");
  153.     }
  154.     /*
  155.      * 修改用户昵称
  156.      */
  157.     public function hx_user_update_nickname($username, $nickname)
  158.     {
  159.         $url = $this->url . "/users/${username}";
  160.         $header = array(
  161.             'Authorization: Bearer ' . $this->token
  162.         );
  163.         $data['nickname'] = $nickname;
  164.         return $this->curl($url, $data, $header, "PUT");
  165.     }
  166.     /*
  167.      *
  168.      * curl
  169.      */
  170.     private function curl($url, $data, $header = false, $method = "POST")
  171.     {
  172.         $ch = curl_init($url);
  173.         curl_setopt($ch, CURLOPT_URL, $url);
  174.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  175.         if ($header) {
  176.             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  177.         }
  178.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  179.         if ($data) {
  180.             curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  181.         }
  182.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  183.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  184.         $ret = curl_exec($ch);
  185.         return $ret;
  186.     }
  187. }

  188. $rs = new Hxcall();
  189. // 注册的用户
  190. //echo $rs->hx_register('qwerasd', 'qazwsx', '福州123' );
  191. // 给IM用户的添加好友
  192. // echo $rs->hx_contacts('admin888', 'qwerasd');
  193. /* 发送文本消息 */
  194. // echo $rs->hx_send('213123','admin888','dfadsr214wefaedf');
  195. /* 消息数统计 */
  196. // echo $rs->hx_msg_count('admin888');
  197. /* 获取IM用户[单个] */
  198. // echo $rs->hx_user_info('admin888');
  199. /* 获取IM用户[批量] */
  200.  echo $rs->hx_user_infos('20');
  201. /* 删除IM用户[单个] */
  202. // echo $rs->hx_user_delete('wwwwww');
  203. /* 修改用户昵称 */
  204. // echo $rs->hx_user_update_nickname('asaxcfasdd','网络科技');
  205. /* 重置IM用户密码 */
  206. // echo $rs->hx_user_update_password('asaxcfasdd','asdad');
  207. /* 解除IM用户的好友关系 */
  208. // echo $rs->hx_contacts_delete('admin888', 'qqqqqqqq');
  209. /* 查看好友 */
  210. //echo $rs->hx_contacts_user('admin888');
复制代码
评论(7 相关
回复xy12303月02日
如何把聊天的数据导出到本地服务器呢
回复王盼01月18日
很好 找了好久
回复baikeliang01月07日
不过稍微有个小问题,就是环信token在有效期内都是可用的,同一账号发送此请求超过一定频率会被服务器封号,切记,切记!建议楼主做一个缓存处理
回复baikeliang01月07日
很好,这个demo可以放到环信的官网上了,官网都没有php的demo
回复在走‘、11月30日
刚做到这个,App需要个聊天功能..先感谢再看
回复郑英明08月12日
这是在给你们公司打广告吗!
回复liupan1822015年06月05日
还不错
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【威信 wēixìn 微信】
这个 App 可能是文艺青年们的「漂流瓶」
3分钟入门微信小程序直播
ActiveMQ 即时通讯服务 浅析(一)
被微信改变的生活:产生“微信孤独症”“点赞之交”异变
天天聊微信的你,知道它背后这些秘密么?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服