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'));