How does one go about creating a database through the cpanel/whm API?

心不动则不痛 提交于 2019-12-06 12:32:57

You can not use root account to create cPanel databases. Also, make sure to set the port. 2082 is for unencrypted connection, while 2083 is for encrypted. You can also use the IP address in place of "domainName".

Check the following code as it should work.

require("xmlapi.php");

$opts = [
    "userName"   => "UserUserName",      //+++ Replace UserUserName
    "password"   => "UserPassword",      //+++ Replace UserPassword
    "dbPassword" => "DatabasePassword",  //+++ Replace DatabasePassword
];

$xmlapi = new xmlapi("domainName");   
$xmlapi->set_port( 2083 );   
$xmlapi->password_auth($opts['userName'],$opts['password']);     

$cpaneluser=$opts['userName'];
$databasename="dbName";
$databaseuser="dbUserName";
$databasepass=$opts['dbPassword'];

// database creation    
$createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename));   
// user creation
$usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));   
// adds user to database
$addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", array("".$cpaneluser."_".$databasename."", "".$cpaneluser."_".$databaseuser."", 'all'));

Thanks @Topher, however a minor suggestion. It is safe to create the user first and then the db:

// user creation
$usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));   

and the db,

// database creation    
$createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename)); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!