I need to create an ejabberd user from a PHP script. I also need to be able to add the new user to a predefined shared roster.
Should I just call ejabberdctl<
I came upon this question in 2016, there are much easier ways to implement this than the accepted answer and the highest voted one.
https://github.com/fabiang/xmpp
here is the class I wrote for adding a user:
use Fabiang\Xmpp\Util\XML;
/**
* Register new user
* @param string $username
* @param string $password
* @param string $email
* @package XMPP\Protocol
* @category XMPP
*/
class Register implements ProtocolImplementationInterface
{
protected $username;
protected $password;
protected $email;
/**
* Constructor.
*
* @param string $username
* @param string $password
* @param string $email
*/
public function __construct($username, $password, $email)
{
$this->username = $username;
$this->password = $password;
$this->email = $email;
}
/**
* Build XML message
* @return type
*/
public function toString()
{
$query = "%s %s %s ";
return XML::quoteMessage($query, XML::generateId(), (string) $this->username, (string) $this->password, (string) $this->email);
}
}
{access, register, [{allow, all}]}.
Finally here is a sample code for using this class:
private function registerChatUser($name, $password, $email)
{
$address = 'tcp://yourserverip:5222';
$adminUsername = 'youradmin';
$adminPassword = 'youradminpassword';
$options = new Options($address);
$options->setUsername($adminUsername)->setPassword($adminPassword);
$client = new Client($options);
$client->connect();
$register = new Register($name, $password, $email);
$client->send($register);
$client->disconnect();
}
The library call will fail if the server does not have a valid SSL certificate. Either place a valid certificate, or replace this part in SocketClient.php with the below snippet
// call stream_socket_client with custom error handler enabled
$handler = new ErrorHandler(
function ($address, $timeout, $flags) {
$options = [
'ssl' => [
'allow_self_signed' => true,
'verify_peer_name' => false,
],
];
$context = stream_context_create($options);
return stream_socket_client($address, $errno, $errstr, $timeout, $flags, $context);
},
$this->address,
$timeout,
$flags
);