PHP registers ejabberd users through xmlrpc and is developing a social software. the architecture im uses ejabberd as an xmpp server, so it encounters the issue of registering xmpp users through php.
There are several solutions:
1. use the xmpphp framework to sendMetadata messages must be processed on the server. for details, refer:
2. use php's xmpp Library jaxl. its demo code contains a register_user script, which is called through shell:
php example/register_user.php YOUR_DOMAIN
Users can be generated. The disadvantage is that the performance is poor and slow, which is not recommended.
3. the optimal method is to use ejabberd's command line tool ejabberdctl to directly generate users. The answer on the internet is to modify the sudo user group permission to directly execute this command using the exec statement in php.
$username = 'tester';$password = 'testerspassword';$node = 'myserver.com';exec('sudo /usr/sbin/ejabberdctl register '.$username.' '.$node.' '.$password.' 2>&1',$output,$status);if($output == 0){ // Success!}else{ // Failure, $output has the details echo ''; foreach($output as $o) { echo $o."\n"; } echo '';}
You need to add the ejabberd user permission to the sudoer file, which is relatively insecure and troublesome, and is not recommended.
In fact, ejabberd has integrated the xmlrpc module in recent versions. through this module, you can directly access port 4560 to use some internal commands of ejabberd. Official website introduction address: https://www.ejabberd.im/ejabberd_xmlrpc
Because I use macos to download the one-click installation package on the ejabberd official website, after installation, I need to cd to the/Application/ejabberd_PATH/conf/folder to modify ejabberd. in the yml configuration file, find the xml_rpc line in the module and remove # (uncomment). after restart, telnet the HOST address 4560 to check whether it can be connected. This indicates that xmlrpc is ready for use.
The php code has been mentioned in the introduction address. The following is the code for php to register a user's demo through ejabberdctl:
$params=array('user'=>'someUser','host'=>'ejabberdHost','password'=>'somPassword');$request = xmlrpc_encode_request('register', $params, (array('encoding' => 'utf-8')));$context = stream_context_create(array('http' => array('method' => "POST",'header' => "User-Agent: XMLRPC::Client mod_xmlrpc\r\n" ."Content-Type: text/xml\r\n" ."Content-Length: ".strlen($request),'content' => $request)));$file = file_get_contents("http://127.0.0.1:4560", false, $context);$response = xmlrpc_decode($file);if (xmlrpc_is_fault($response)) {trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");} else {print_r($response);}
The registration successful result is generated after printing.
Have a nice try! :)