PHP registers ejabberd users through xmlrpc

Source: Internet
Author: User
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! :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.