API 2 how to create new database and set existing user Privilege

cPanel & WHM Version
86.0.18

kvcodes

Registered
Apr 26, 2020
2
2
1
India
cPanel Access Level
Website Owner
We are trying to create database through api 2. but we can't acheieve it. How do we require the class Cpanel .

Here is our tried code. but didnt work.



$cpanel = new CPANEL(); // Connect to cPanel - only do this once.

$database = 'exampl_db';
$create_database = $cpanel->api2(
'MysqlFE', 'createdb',
array(
'db' => $database,
)
);
if($create_database == 1) {

$grant_user = $cpanel->api2(
'MysqlFE', 'setdbuserprivileges',
array(
'privileges' => 'ALL PRIVILEGES',
'db' => $database,
'dbuser' => 'username',
)
);
and we got this error " Fatal error: Uncaught Error: Class 'CPANEL' not found in" So we tried to require the cpanel program.


require_once "/usr/local/cpanel/php/cpanel.php"; on the same file
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,295
1,257
313
Houston
  • Like
Reactions: cPanelTJ