环信常用接口封装
<?php
class Easemob
{
private $client_id = '';
private $client_secret = "";
private $org_name = "";
private $url = '';
public function __construct($options)
{
$this->client_id = isset ($options ['client_id']) ? $options ['client_id'] : '';
$this->client_secret = isset ($options ['client_secret']) ? $options ['client_secret'] : '';
$this->org_name = isset ($options ['org_name']) ? $options ['org_name'] : '';
$this->app_name = isset ($options ['app_name']) ? $options ['app_name'] : '';
if (!empty ($this->org_name) && !empty ($this->app_name)) {
$this->url = 'https://a1.easemob.com/' . $this->org_name . '/' . $this->app_name;
}
}
function huanxin_curl_request($url, $body, $header = array(), $method = "POST") {
array_push ( $header, 'Accept:application/json' );
array_push ( $header, 'Content-Type:application/json' );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
switch (strtoupper($method)) {
case "GET" :
curl_setopt ( $ch, CURLOPT_HTTPGET, true );
break;
case "POST" :
curl_setopt ( $ch, CURLOPT_POST, true );
break;
case "PUT" :
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "PUT" );
break;
case "DELETE" :
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "DELETE" );
break;
}
curl_setopt ( $ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0' );
curl_setopt ( $ch, CURLOPT_ENCODING, 'gzip' );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 1 );
if (isset ( $body {3} ) > 0) {
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $body );
}
if (count ( $header ) > 0) {
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
}
$ret = curl_exec ( $ch );
$err = curl_error ( $ch );
curl_close ( $ch );
if ($err) {
return $err;
}
return $ret;
}
function postCurl($url,$body,$header,$type="POST"){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt ( $ch, CURLOPT_TIMEOUT,5);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (count($body)>0) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
if(count($header)>0){
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
switch($type){
case "GET":
curl_setopt($ch,CURLOPT_HTTPGET,true);
break;
case "POST":
curl_setopt($ch,CURLOPT_POST,true);
break;
case "PUT":
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"PUT");
break;
case "DELETE":
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"DELETE");
break;
}
curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' );
$res=curl_exec($ch);
$result=json_decode($res,true);
curl_close($ch);
if(empty($result))
return $res;
else
return $result;
}
function getTokens($force = false)
{
$url = $this->url."/token";
$options=array(
"grant_type"=>"client_credentials",
"client_id"=> $this->client_id,
"client_secret"=> $this->client_secret
);
$body=json_encode($options);
$tokenResult = postCurl($url,$body);
return "Authorization:Bearer ". $tokenResult["access_token"];
}
function huanxin_zhuce($username, $password)
{
$url = $this->url . "/users";
$param = array(
"username" => $username,
"password" => $password
);
$res = huanxin_curl_request($url, json_encode($param));
$tokenResult = json_decode($res, true);
$tokenResult["password"] = $param["password"];
$huanxin_uuid = $tokenResult["entities"][0]["uuid"];
$huanxin_username = $tokenResult["entities"][0]["username"];
$huanxin_password = $param["password"];
if (!($huanxin_uuid && $huanxin_username)) {
return false;
} else {
return true;
}
}
function createUsers($options){
$url=$this->url.'users';
$body=json_encode($options);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header);
return $result;
}
function resetPassword($username,$newpassword){
$url=$this->url.'users/'.$username.'/password';
$options=array(
"newpassword"=>$newpassword
);
$body=json_encode($options);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header,"PUT");
return $result;
}
function getUser($username){
$url=$this->url.'users/'.$username;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,"GET");
return $result;
}
function getUsers($limit=0){
if(!empty($limit)){
$url=$this->url.'users?limit='.$limit;
}else{
$url=$this->url.'users';
}
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,"GET");
return $result;
}
function getUsersForPage($limit=0,$cursor=''){
$url=$this->url.'users?limit='.$limit.'&cursor='.$cursor;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,"GET");
if(!empty($result["cursor"])){
$cursor=$result["cursor"];
$this->writeCursor("userfile.txt",$cursor);
}
return $result;
}
function mkdirs($dir, $mode = 0777)
{
if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
if (!mkdirs(dirname($dir), $mode)) return FALSE;
return @mkdir($dir, $mode);
}
function writeCursor($filename,$content){
if(!file_exists("resource/txtfile")){
mkdirs("resource/txtfile");
}
$myfile=@fopen("resource/txtfile/".$filename,"w+") or die("Unable to open file!");
@fwrite($myfile,$content);
fclose($myfile);
}
function readCursor($filename){
if(!file_exists("resource/txtfile")){
mkdirs("resource/txtfile");
}
$file="resource/txtfile/".$filename;
$fp=fopen($file,"a+");
if($fp){
while(!feof($fp)){
$data=fread($fp,1000);
}
fclose($fp);
}
return $data;
}
function deleteUser($username){
$url=$this->url.'users/'.$username;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function deleteUsers($limit){
$url=$this->url.'users?limit='.$limit;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function editNickname($username,$nickname){
$url=$this->url.'users/'.$username;
$options=array(
"nickname"=>$nickname
);
$body=json_encode($options);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header,'PUT');
return $result;
}
function addFriend($username,$friend_name){
$url=$this->url.'users/'.$username.'/contacts/users/'.$friend_name;
$header=array($this->getToken(),'Content-Type:application/json');
$result=$this->postCurl($url,'',$header,'POST');
return $result;
}
function deleteFriend($username,$friend_name){
$url=$this->url.'users/'.$username.'/contacts/users/'.$friend_name;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function showFriends($username){
$url=$this->url.'users/'.$username.'/contacts/users';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function getBlacklist($username){
$url=$this->url.'users/'.$username.'/blocks/users';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function addUserForBlacklist($username,$usernames){
$url=$this->url.'users/'.$username.'/blocks/users';
$body=json_encode($usernames);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header,'POST');
return $result;
}
function deleteUserFromBlacklist($username,$blocked_name){
$url=$this->url.'users/'.$username.'/blocks/users/'.$blocked_name;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function isOnline($username){
$url=$this->url.'users/'.$username.'/status';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function getOfflineMessages($username){
$url=$this->url.'users/'.$username.'/offline_msg_count';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function getOfflineMessageStatus($username,$msg_id){
$url=$this->url.'users/'.$username.'/offline_msg_status/'.$msg_id;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function deactiveUser($username){
$url=$this->url.'users/'.$username.'/deactivate';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header);
return $result;
}
function activeUser($username){
$url=$this->url.'users/'.$username.'/activate';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header);
return $result;
}
function disconnectUser($username){
$url=$this->url.'users/'.$username.'/disconnect';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function uploadFile($filePath){
$url=$this->url.'chatfiles';
$file=file_get_contents($filePath);
$body['file']=$file;
$header=array('enctype:multipart/form-data',$this->getToken(),"restrict-access:true");
$result=$this->postCurl($url,$body,$header,'XXX');
return $result;
}
function downloadFile($uuid,$shareSecret){
$url=$this->url.'chatfiles/'.$uuid;
$header = array("share-secret:".$shareSecret,"Accept:application/octet-stream",$this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
$filename = md5(time().mt_rand(10, 99)).".png";
if(!file_exists("resource/down")){
mkdirs("resource/down/");
}
$file = @fopen("resource/down/".$filename,"w+");
@fwrite($file,$result);
fclose($file);
return $filename;
}
function downloadThumbnail($uuid,$shareSecret){
$url=$this->url.'chatfiles/'.$uuid;
$header = array("share-secret:".$shareSecret,"Accept:application/octet-stream",$this->getToken(),"thumbnail:true");
$result=$this->postCurl($url,'',$header,'GET');
$filename = md5(time().mt_rand(10, 99))."th.png";
if(!file_exists("resource/down")){
mkdirs("resource/down/");
}
$file = @fopen("resource/down/".$filename,"w+");
@fwrite($file,$result);
fclose($file);
return $filename;
}
function sendText($from="admin",$target_type,$target,$content,$ext){
$url=$this->url.'messages';
$body['target_type']=$target_type;
$body['target']=$target;
$options['type']="txt";
$options['msg']=$content;
$body['msg']=$options;
$body['from']=$from;
$body['ext']=$ext;
$b=json_encode($body);
$header=array($this->getToken());
$result=$this->postCurl($url,$b,$header);
return $result;
}
function sendCmd($from="admin",$target_type,$target,$action,$ext){
$url=$this->url.'messages';
$body['target_type']=$target_type;
$body['target']=$target;
$options['type']="cmd";
$options['action']=$action;
$body['msg']=$options;
$body['from']=$from;
$body['ext']=$ext;
$b=json_encode($body);
$header=array($this->getToken());
$result=$this->postCurl($url,$b,$header);
return $result;
}
function sendImage($filePath,$from="admin",$target_type,$target,$filename,$ext){
$result=$this->uploadFile($filePath);
$uri=$result['uri'];
$uuid=$result['entities'][0]['uuid'];
$shareSecret=$result['entities'][0]['share-secret'];
$url=$this->url.'messages';
$body['target_type']=$target_type;
$body['target']=$target;
$options['type']="img";
$options['url']=$uri.'/'.$uuid;
$options['filename']=$filename;
$options['secret']=$shareSecret;
$options['size']=array(
"width"=>480,
"height"=>720
);
$body['msg']=$options;
$body['from']=$from;
$body['ext']=$ext;
$b=json_encode($body);
$header=array($this->getToken());
$result=$this->postCurl($url,$b,$header);
return $result;
}
function sendAudio($filePath,$from="admin",$target_type,$target,$filename,$length,$ext){
$result=$this->uploadFile($filePath);
$uri=$result['uri'];
$uuid=$result['entities'][0]['uuid'];
$shareSecret=$result['entities'][0]['share-secret'];
$url=$this->url.'messages';
$body['target_type']=$target_type;
$body['target']=$target;
$options['type']="audio";
$options['url']=$uri.'/'.$uuid;
$options['filename']=$filename;
$options['length']=$length;
$options['secret']=$shareSecret;
$body['msg']=$options;
$body['from']=$from;
$body['ext']=$ext;
$b=json_encode($body);
$header=array($this->getToken());
$result=$this->postCurl($url,$b,$header);
return $result;
}
function sendVedio($filePath,$from="admin",$target_type,$target,$filename,$length,$thumb,$thumb_secret,$ext){
$result=$this->uploadFile($filePath);
$uri=$result['uri'];
$uuid=$result['entities'][0]['uuid'];
$shareSecret=$result['entities'][0]['share-secret'];
$url=$this->url.'messages';
$body['target_type']=$target_type;
$body['target']=$target;
$options['type']="video";
$options['url']=$uri.'/'.$uuid;
$options['filename']=$filename;
$options['thumb']=$thumb;
$options['length']=$length;
$options['secret']=$shareSecret;
$options['thumb_secret']=$thumb_secret;
$body['msg']=$options;
$body['from']=$from;
$body['ext']=$ext;
$b=json_encode($body);
$header=array($this->getToken());
$result=$this->postCurl($url,$b,$header);
return $result;
}
function sendFile($filePath,$from="admin",$target_type,$target,$filename,$length,$ext){
$result=$this->uploadFile($filePath);
$uri=$result['uri'];
$uuid=$result['entities'][0]['uuid'];
$shareSecret=$result['entities'][0]['share-secret'];
$url=$GLOBALS['base_url'].'messages';
$body['target_type']=$target_type;
$body['target']=$target;
$options['type']="file";
$options['url']=$uri.'/'.$uuid;
$options['filename']=$filename;
$options['length']=$length;
$options['secret']=$shareSecret;
$body['msg']=$options;
$body['from']=$from;
$body['ext']=$ext;
$b=json_encode($body);
$header=array(getToken());
$result=postCurl($url,$b,$header);
return $result;
}
function getGroups($limit=0){
if(!empty($limit)){
$url=$this->url.'chatgroups?limit='.$limit;
}else{
$url=$this->url.'chatgroups';
}
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,"GET");
return $result;
}
function getGroupsForPage($limit=0,$cursor=''){
$url=$this->url.'chatgroups?limit='.$limit.'&cursor='.$cursor;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,"GET");
if(!empty($result["cursor"])){
$cursor=$result["cursor"];
$this->writeCursor("groupfile.txt",$cursor);
}
return $result;
}
function createGroup($options){
$url=$this->url.'chatgroups';
$header=array($this->getToken());
$body=json_encode($options);
$result=$this->postCurl($url,$body,$header);
return $result;
}
function modifyGroupInfo($group_id,$options){
$url=$this->url.'chatgroups/'.$group_id;
$body=json_encode($options);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header,'PUT');
return $result;
}
function deleteGroup($group_id){
$url=$this->url.'chatgroups/'.$group_id;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function getGroupUsers($group_id){
$url=$this->url.'chatgroups/'.$group_id.'/users';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function addGroupMember($group_id,$username){
$url=$this->url.'chatgroups/'.$group_id.'/users/'.$username;
$header=array($this->getToken(),'Content-Type:application/json');
$result=$this->postCurl($url,'',$header);
return $result;
}
function addGroupMembers($group_id,$usernames){
$url=$this->url.'chatgroups/'.$group_id.'/users';
$body=json_encode($usernames);
$header=array($this->getToken(),'Content-Type:application/json');
$result=$this->postCurl($url,$body,$header);
return $result;
}
function deleteGroupMember($group_id,$username){
$url=$this->url.'chatgroups/'.$group_id.'/users/'.$username;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function deleteGroupMembers($group_id,$usernames){
$url=$this->url.'chatgroups/'.$group_id.'/users/'.$usernames;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function getGroupsForUser($username){
$url=$this->url.'users/'.$username.'/joined_chatgroups';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function changeGroupOwner($group_id,$options){
$url=$this->url.'chatgroups/'.$group_id;
$body=json_encode($options);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header,'PUT');
return $result;
}
function getGroupBlackList($group_id){
$url=$this->url.'chatgroups/'.$group_id.'/blocks/users';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function addGroupBlackMember($group_id,$username){
$url=$this->url.'chatgroups/'.$group_id.'/blocks/users/'.$username;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header);
return $result;
}
function deleteGroupBlackMember($group_id,$username){
$url=$this->url.'chatgroups/'.$group_id.'/blocks/users/'.$username;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function deleteGroupBlackMembers($group_id,$usernames){
$url=$this->url.'chatgroups/'.$group_id.'/blocks/users';
$body=json_encode($usernames);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header,'DELETE');
return $result;
}
function createChatRoom($options){
$url=$this->url."/chatrooms";
$header=array(getTokens());
$body=json_encode($options);
$result=postCurl($url,$body,$header);
return $result;
}
public function get_chatrooms($create_room){
$token = getTokens();
$url=$this->url.'chatrooms/'.$create_room;
$body = "";
$header = array($token);
$chatroomsinfo = huanxin_curl_request($url, $body, $header, $method = "get");
$roominfo = json_decode($chatroomsinfo,true);
$count = $roominfo["data"][0]["affiliations_count"];
return $chatroomsinfo;
}
function modifyChatRoom($chatroom_id,$options){
$url=$this->url.'chatrooms/'.$chatroom_id;
$body=json_encode($options);
$result=$this->postCurl($url,$body,$header,'PUT');
return $result;
}
function deleteChatRoom($chatroom_id){
$url=$this->url.'chatrooms/'.$chatroom_id;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function getChatRooms(){
$url=$this->url.'chatrooms';
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,"GET");
return $result;
}
function getChatRoomDetail($chatroom_id){
$url=$this->url.'chatrooms/'.$chatroom_id;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'GET');
return $result;
}
function addChatRoomMember($chatroom_id,$username){
$url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$username;
$header=array($this->getToken(),'Content-Type:application/json');
$result=$this->postCurl($url,'',$header);
return $result;
}
function addChatRoomMembers($chatroom_id,$usernames){
$url=$this->url.'chatrooms/'.$chatroom_id.'/users';
$body=json_encode($usernames);
$header=array($this->getToken());
$result=$this->postCurl($url,$body,$header);
return $result;
}
function deleteChatRoomMember($chatroom_id,$username){
$url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$username;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
function deleteChatRoomMembers($chatroom_id,$usernames){
$url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$usernames;
$header=array($this->getToken());
$result=$this->postCurl($url,'',$header,'DELETE');
return $result;
}
}