环信用户导入PHP

<?php
/**
 * 环信公用
 *
 * @package  library  
 */
defined('emall') or exit('Access Invalid!');


class Easemob {


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


    /**
     * 更改用户昵称
     * @param  [string] $username [用户名称]
     * @param  [string] $nickname [用户昵称]
     *
     */
public function editNick($username,$nickname) {
        $formgettoken = C('easemob.base_url')."users/" . $username;
        $body=array(
            "username"=>$username,
            "nickname"=>$nickname,
        );
        $patoken=json_encode($body);
        $header = array($this->_get_token_easemob());
        $result = $this->_curl_request( $formgettoken, $patoken, $header, $type = 'PUT');
        return $result;
    }
    /**
     * 授权注册模式 || 批量注册
     *
     * @param $options['username'] 用户名          
     * @param $options['password'] 密码
     *          批量注册传二维数组
     */
public  function accreditRegister($options) {
        $formgettoken = C('easemob.base_url')."users";
        $header = array($this->_get_token_easemob());
        $result = $this->_curl_request ( $formgettoken, json_encode($options), $header );
        return $result;
    }




//授权注册模式 POST /{org_name}/{app_name}/users
public function registerToken($username,$pwd,$nickname='')
{
    $formgettoken=C('easemob.base_url')."users";
    $body=array(
        "username"=>$username,
        "password"=>$pwd,
        'nickname'=>$nickname
    );
    $patoken=json_encode($body);
    $header = array($this->_get_token_easemob());
    $res = $this->_curl_request($formgettoken,$patoken,$header);


    $arrayResult =  json_decode($res, true);    
    return $arrayResult ;
}
//重置用户密码 PUT /{org_name}/{app_name}/users/{username}/password
public function changePwdToken($nikename,$newpwd,$password)
{
    $formgettoken=C('easemob.base_url')."users/".$nikename."/password";
    $body=array(
        "oldpassword" => $password,
        "newpassword"=>$newpwd,
    );
    $patoken=json_encode($body);
    $header = array($this->_get_token_easemob());
    $method = "PUT";
    $res = $this->_curl_request($formgettoken,$patoken,$header,$method);
    $arrayResult =  json_decode($res, true);    
    return $arrayResult ;
}
//删除 DELETE /{org_name}/{app_name}/users/{username}
public function delUserToken($nikename)
{
    $formgettoken=C('easemob.base_url')."users/".$nikename;
    $body=array();
    $patoken=json_encode($body);
    $header = array($this->_get_token_easemob());
    $method = "DELETE";
    $res = $this->_curl_request($formgettoken,$patoken,$header,$method);
    $arrayResult =  json_decode($res, true);    
    return $arrayResult ;
}




//先获取app管理员token POST /{org_name}/{app_name}/token
private function _get_token_easemob()
{
    $formgettoken=C('easemob.base_url')."token";
    $body=array(
    "grant_type"=>"client_credentials",
    "client_id"=>C('easemob.client_id'),
    "client_secret"=>C('easemob.client_secret')
    );
    $patoken=json_encode($body);
    $res = $this->_curl_request($formgettoken,$patoken);
    $tokenResult = array();
    
    $tokenResult =  json_decode($res, true);
    //var_dump($tokenResult);
    return "Authorization: Bearer ". $tokenResult["access_token"];  
}


private function _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);
    //curl_setopt($ch, $method, 1);
    
    switch ($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);
    //$err = curl_getinfo($ch,CURLINFO_HTTP_CODE); //设置返回状态码
    curl_close($ch);
    //clear_object($ch);
    //clear_object($body);
    //clear_object($header);


    if ($err) {
        return $err;
    }


    return $ret;


    }
}
注:C('xxx') //从配置文件中读取

你可能感兴趣的:(环信用户导入PHP)