Example #1
0
/**
 * 创建用户信息,如果用户信息存在则更新
 * 
 * @param string $username 用户名
 * @param string $password 密码
 * @param string $name 昵称
 * @param string $email 邮箱
 * @param string $type 用户类型
 * @param string $shop_id 店铺ID
 * @return boolean
 */
function create_of_user($username = null, $password = null, $name = null, $email = null, $type = 10, $shop_id = -1)
{
    $_CFG = $GLOBALS['_CFG'];
    $of_username = $_CFG['chat_server_admin_username'];
    $of_password = $_CFG['chat_server_admin_password'];
    $of_ip = $_CFG['chat_server_ip'];
    $of_port = $_CFG['chat_server_port'];
    $of_url = get_of_url($of_ip, $of_port);
    if ($username == null || strlen($username) == 0) {
        return false;
    }
    // 判断用户是否已经存在
    $exist = check_of_username_exist($username);
    if ($exist) {
        if ($password == null || strlen($password) == 0) {
            $password = null;
        }
        $url = $of_url . '/plugins/userService/users/' . $username;
        $method = 'PUT';
    } else {
        if ($password == null || strlen($password) == 0) {
            return false;
        }
        $url = $of_url . '/plugins/userService/users';
        $method = 'POST';
    }
    $user = new User();
    $user->username = $username;
    $user->password = $password;
    $user->name = $name;
    $user->email = $email;
    $user->properties = array(new Property('type', $type), new Property('shop_id', $shop_id));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // 设置HTTP头
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    // 授权验证
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $of_username . ":" . $of_password);
    // 设置可以读取返回值
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post提交方式
    // 	curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    // 提交的数据
    // curl_setopt ( $ch, CURLOPT_POSTFIELDS, array('username'=>$username, 'password'=>$password, 'name'=>$name, 'email'=>$email) );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $user->asXML());
    // 运行curl
    $result = trim(curl_exec($ch));
    // 关闭
    curl_close($ch);
    if (strpos($result, '201 Created') >= 0) {
        return true;
    } else {
        if (strpos($result, '400 Bad Request') >= 0) {
            return false;
        } else {
            if (strpos($result, 'UserAlreadyExistsException') >= 0) {
                return true;
            } else {
                return false;
            }
        }
    }
}