How can I create a new Joomla user account from within a script?

后端 未结 12 776
感动是毒
感动是毒 2020-12-01 03:41

We\'re creating an XML API for Joomla that allows partner sites to create new accounts for their users on our website.

We\'ve created a standalone PHP script that

12条回答
  •  不思量自难忘°
    2020-12-01 04:17

    Tested and working on 2.5.

    function addJoomlaUser($name, $username, $password, $email) {
            $data = array(
                "name"=>$name, 
                "username"=>$username, 
                "password"=>$password,
                "password2"=>$password,
                "email"=>$email
            );
    
            $user = clone(JFactory::getUser());
            //Write to database
            if(!$user->bind($data)) {
                throw new Exception("Could not bind data. Error: " . $user->getError());
            }
            if (!$user->save()) {
                throw new Exception("Could not save user. Error: " . $user->getError());
            }
    
            return $user->id;
    }
    

    If you're outside the Joomla environment, you'll have to do this first, or if you're not writing a component, use the one in the link in @GMonC's answer.

    initialise();
    

    I use this for unit testing my component.

提交回复
热议问题