Theme images by Storman. Powered by Blogger.

28 December 2016

How to use API cpanel to create account in php?




Normally we used to create cpanel account by login to their website.and then create. but if we want to build one system that allow user to create
cpanel directly on our system need to fellow this step:

1) Define our information

 <?php
// Prevent the script from timing out
set_time_limit(0);

// Settings
$whmUsername = 'root';
$whmPassword = 'ourpassword'; // our cpanel password
$whmUrl = 'oururl'; // our cpanel url address NOTE if has port please include
$apiMethod = 'json-api/createacct'; // if we want to create please write this
$apiVersion = '1';

// API Method URL
$method = $whmUrl . $apiMethod;

// User Info

// See documentation for full list of parameters:
// https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+createacct
$users = array(
 
    'username'      => 'username', // Required
    'domain'        => 'ourdomainname', // Required
    'password'      => 'ourpassword',
    'cpmod'         => 'paper_lantern',
    'contactemail'  => 'email'
  );



2)  user php curl to create 

// Setup cURL + Basic Auth
$curl = curl_init();

// Allow self signed certificates
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);

curl_setopt($curl, CURLOPT_HEADER,0);

// Basic Authentication
$header[0] = 'Authorization: Basic ' . base64_encode($whmUsername.":".$whmPassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);


  // Set the API version
  $data['api.version'] = $apiVersion;
 
  // Build the query
    $query = http_build_query($users);
 
  // Execute the query
    curl_setopt($curl, CURLOPT_URL, $method . '?' . $query);
    curl_exec($curl);

?>