一个小程序中用到了聊天功能,客户要求使用三方通信,环信,因为是第一次接触,咨询了下我们技术总监,短暂研究后告诉我,我们服务端只需要用到两个功能就好,获取token和注册用户功能即可,其他功能,前端集成小程序的SDK即可完成,于是我找到文档,文档地址 http://api-docs.easemob.com/#/,一看文档,很简单,直接请求接口就好了,就喜欢这种简单粗暴的方式。下面简单写几个接口的示例。
1.获取token
public function getToken($reGet=false) { $data = array( 'grant_type' => 'client_credentials', 'client_id' => 'XXX', //环信 'client_secret' => 'XXX' //环信 ); $data_string = json_encode($data); $path='http://a1.easemob.com/"Orgname"/"appname"/token'; //带引号的填写环信对应参数即可 $ch = curl_init($path); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt ($ch, CURLOPT_POSTFIELDS,$data_string); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查 curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在 curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器 curl_setopt ($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt ($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json')); $result = curl_exec($ch); $result_arr = json_decode($result, true); $token=$result_arr['access_token']; return $token; }
2.注册环信用户
public function setUser() { $uid=112; $data = array( 'username' => $uid, //环信用户名,也为用户id,还有个昵称参数,我没有用到 'password' => '123456', //密码 ); $token=$this->getToken(); //此处调用的上面的获取token接口 $data_string = json_encode($data); $path='http://a1.easemob.com/1/2/users'; //1和2处填写环信对应参数 $ch = curl_init($path); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt ($ch, CURLOPT_POSTFIELDS,$data_string); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查 curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在 curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器 curl_setopt ($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt ($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json',"Authorization:$token")); $result = curl_exec($ch); $result_arr = json_decode($result, true); if(isset($result_arr[$data])){
return json(['code']=>200,'msg'=>'成功);
}else{
return json(['code']=>400,'msg'=>'失败');
} }
3.获取好友关系
#获取好友关系 public function getUser() { $uid=126; if(!$uid){ return json(['code'=>401,'msg'=>'请重新登录']); } $token=$this->getToken(); //此处调用上面获取token的方法 $tokens="Bearer ".$token; $path="http://a1.easemob.com/1/2/users/$uid/contacts/users"; //1和2处填写对应环信参数 $ch = curl_init($path); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查 curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在 curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器 curl_setopt ($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt ($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json',"Authorization:$tokens","owner_username:$uid")); $result = curl_exec($ch); $result_arr = json_decode($result, true); if(isset($result_arr['data])){
$data=$result_arr['data'];
return json(['code'=>200,'msg'=>'成功','data'=>$arr]);
}else{
return json(['code'=>400,'msg'=>'失败','data'=>$result_arr ]);
} }
4.加好友
public function addUser($uid,$uids) { //此处uid和uids皆为环信的用户id $token=$this->getToken(); //此处调用获取token方法 $tokens="Bearer ".$token; $path="http://a1.easemob.com/1/2/users/$uid/contacts/users/$uids"; //此处1和2皆为环信对应参数 $ch = curl_init($path); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查 curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在 curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器 curl_setopt ($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt ($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json',"Authorization:$tokens","owner_username:$uid","friend_username:$uids")); $result = curl_exec($ch); $result_arr = json_decode($result, true); if(isset($result_arr['path'])){ return json(['code'=>200,'msg'=>'成功']); }else{ return json(['code'=>400,'msg'=>'失败']); } }
5.删除好友
#删除好友 public function delUser() { $uid=111; $uids=124; //要解除的好友关系id $token=$this->getToken(); //此处为获取token方法 $tokens="Bearer ".$token; //因为这个方法 token有前置字符串 做了个拼接 $path="http://a1.easemob.com/1/2/users/$uid/contacts/users/$uids"; //此处1和2皆为环信对应参数 $ch = curl_init($path); curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查 curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在 curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器 curl_setopt ($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt ($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json',"Authorization:$tokens","owner_username:$uid","friend_username:$uids")); $result = curl_exec($ch); $result_arr = json_decode($result, true); }
其他方法也是类似,就不在多做举例,按照这个格式就好,别忘记请求方式不同,post,get,delete等