is developing a social software, architecture im using Ejabberd as the XMPP server, then encountered how to register XMPP users through PHP problem.
There are several workarounds:
1. Using the xmpphp framework to send messages containing meta-data to the server requires processing, this can be consulted:
2. Using the PHP XMPP library Jaxl, the demo code contains a Register_user script that is called through the shell:
PHP example/register_user.php Your_domain
Users can be generated, the disadvantage is poor performance, not recommended
3. The best approach is to use Ejabberd's own command-line tool EJABBERDCTL to generate the user directly. The answer on the web is basically to use the EXEC statement to execute this command directly in PHP by modifying the sudo user group permissions
$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 '
';}
The need to add ejabberd user rights in the Sudoer file is relatively unsafe and cumbersome, and not recommended.
In fact, Ejabberd has integrated the XMLRPC module in the latest version, which provides direct access to 4560 ports using some of the internal commands of Ejabberd. Official website address: HTTPS://WWW.EJABBERD.IM/EJABBERD_XMLRPC
Because I use MacOS on the Ejabberd official website Download the one-click installation package, after the installation required CD to the/application/ejabberd_path/conf/folder to modify the Ejabberd.yml configuration file, found in module Xml_ RPC Line minus # (uncomment), reboot after the Telnet host address 4560 to see if it can be connected, that is, XMLRPC is ready to use
On the PHP side of the code in the introduction address has been mentioned, the following is PHP through EJABBERDCTL registration of a user's demo code:
$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 result of successful registration is generated after printing
Had a nice try! :)