How to create user using REST APi?
How to create user using REST APi?
This is something I wrote quickly to bulk add users from my company database to rocket.chat
`<?php
add users to an array.
$user = array(array('email' => '[email protected]', 'pass' => 'somepassword', 'name' => 'some.username'));
$users =array('users' => $user);
login function
function talklogin($fuser, $pass) {
$service_url = 'http://your.rocketchat.url/api/login'; $curl = curl_init($service_url); $curl_post_data = array( "password" => $pass, "user" => $fuser, ); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curl_post_data)); $curl_response = curl_exec($curl); curl_close($curl); return json_decode($curl_response); }
create user function, it takes array of users and login token as parameters.
function createuser($jusers, $me) { $service_url = 'http://your.rocketchat.url/api/bulk/register'; $curl = curl_init($service_url); $curl_post_data = json_encode($jusers); echo $curl_post_data; curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'X-Auth-Token: '.$me->data->authToken, 'X-User-Id: '.$me->data->userId, "Content-Type: application/json" )); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); $curl_response = curl_exec($curl); curl_close($curl); return json_decode($curl_response); }
do the login
$me = talklogin('youradminuser', 'youradminpassword');
if login is OK, create users ...
if ($me->status == 'success') { echo 'loggedin:'; $de = createuser($users, $me); print_r($de); } else { echo 'error: '. $de->status; }
`
Please see here: https://rocket.chat/docs/developer-guides/rest-api/ for details on using the rest api.