Knowledgebase
Create new user with API - custom signup form
Posted by Conal Mullan on 24 August 2015 02:12 PM

This article provides a basic example on how to add a new user via the OnSync API.

The API allows for complete control over the creation of new users and developers have a large scope to create sophiscated solutions using any language whch can make REST http requests. 

This is a working example in PHP using cURL to make the REST requests - it is kept purposely simple as possible.

-----------------------------------------------------------------------------------------------

To create a new user, we use the the PUT user method of the API. 

This method is detailed in its entirety here: https://docs.google.com/document/d/1oajGxlr0kAfzol0zFc7ZjPt5cbtr44qk8hLkuO0gRQw/edit#bookmark=id.ti9blhv3b361

1. I am going to use the 5 required arguments for this method and generate a basic html form to capture them from the end user. These 5 required fields are follows:

login (string)
password (string)
email (string)
first_name (string)
last_name (string)

--------------------------------------------------------------------------------------------------------------

<form id='register' action='register.php' method='post' accept-charset='UTF-8'>
<fieldset >
<legend>Register</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='first_name'>First Name*: </label>
<input type='text' name='first_name' id='first_name' maxlength="50" />
<label for='last_name'>Last Name*: </label>
<input type='text' name='last_name' id='last_name' maxlength="50" />
<label for='email'>Email Address*:</label>
<input type='text' name='email' id='email' maxlength="50" />

<label for='login'>Desired Username*:</label>
<input type='text' name='login' id='username' maxlength="50" />

<label for='password' >Password*:</label>
<input type='password' name='password' id='password' maxlength="50" />
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>


 2. This form with be submitted to register.php where the API code resides 

NOTE: Although the API will validate any arguments passed to it, we encourage you to include some level of validation in your own code before passing any values to the API. 

------------------------------------------------------------------------------------------------------------------------

// php code for register.php

if(isset($_POST['submitted']))
{
// form has been submitted
// requires admin user rights with API enabled

$username = '[__YOUR_USERNAME__]'; // update as appropriate
$password = '[__YOUR_PASSWORD__]'; // update as appropriate
$baseurl = 'http://[__YOUR_URL__]/api/2/'; // update as appropriate

$parameters = array(
'login' => $_POST['login'],
'password' => $_POST['password'],
'email' => $_POST['email'],
'first_name'=> $_POST['first_name'],
'last_name'=> $_POST['last_name']
// 'service_plan_id' => 4 // if the service_plan_id value is not sent specified - your default service plan will be used.
);

// encode as JSON
$json = json_encode($parameters);
$postArgs = 'input_type=json&rest_data=' . $json;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $baseurl.$username.'/user/format/php');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERPWD, $username.':'.$password);
$result = curl_exec($curl);
curl_close($curl);
eval ('$results = ' . $result . ';'); // convert return string to php array

if ( isset($results['id']) )
{
// upon successful creation of an account. You will be returned an 'id' value as part of return array
echo ('User successfully created - your user id is:'.$results['id']);
}
else
{
// should the user creation fail for any reason - you will be returned a 'error' value which is a string detailing the problem
echo ('Unable to create new user - reason:<b> '.$results['error'].'</b>');
}
}



NOTE: If you do not specify a service_plan_id as part of your request parameters - your configurations default service plan will be used.

i.e. If you want to allocate your new users a specific plan through your code which is not the default service plan - you can send the required service plan's id as part of the request parameters (service_plan_id) that you are sending to the API.

You can reveiw you service plan ids by logging in as Admin and reveiwing the id column after clicking on the Service Plans button.

You can also retrieve Service Plan details through the the GET service_plans API method -> https://docs.google.com/document/d/1oajGxlr0kAfzol0zFc7ZjPt5cbtr44qk8hLkuO0gRQw/edit#bookmark=id.v0qmhtc8lhex

 

(1 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).

Thank you for choosing OnSync.
For OnSync product information, please go to the Digital Samba website.
www.digitalsamba.com © Digital Samba, SL.