2

I have the API creating an account on a dedicated server for a user fine, creating email addresses fine as well, however when I attempt to create an mysql database through the API, I get error message 'Access Denied', even though access is root.

The code I am trying to use:

$xmlapi = new xmlapi($host);
$xmlapi->password_auth("".$root_user."","".$root_pass."");    
$xmlapi->set_debug(1);
$xmlapi->set_output('array');
$xmlapi->set_port('2083'); 
//create database
$createdb = $xmlapi->api1_query($root_user, "Mysql", "adddb", array($dbname));

and the error data I am getting back:

URL: https://delta1.powerstorm.net:2083/xml-api/cpanel
DATA: cpanel_xmlapi_user=root&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adddb&cpanel_xmlapi_apiversion=1&arg-0=site
Authentication Header: Authorization: Basic cm9vdDphY3Jtp3MxOTY1

RESPONSE:
 <?xml version="1.0" ?>
<cpanelresult>
    <error>Access denied</error>
    <data>
        <result>0</result>
        <reason>Access denied</reason>
    </data>
</cpanelresult>

Any ideas on why this one part is failing when everything else works fine?

Jaime Cross
  • 523
  • 1
  • 3
  • 15
  • Why would one even want to do something like this? My guess though is that you are confusing your server user account with a MySQL user account. They are not the same thing. – Mike Brant Dec 06 '13 at 20:56
  • Hi Mike, what I am trying to do is a one stop account creation, wordpress install, and theme install. The API documentation is vague at best for the v1 api, and the error messages are worse. I tried changing the database user/pass to same as server creds, still fails with same message. – Jaime Cross Dec 06 '13 at 21:05
  • 1
    I guess I am wondering why one would use the cPanel API at all to create a new user/database when one can likely do it much more easily via an SQL script run from shell script, direct interaction with MySQL in PHP, etc. What value does the cPanel API bring to this process with regards to MySQL? – Mike Brant Dec 06 '13 at 22:09
  • @MikeBrant I tried doing it the easy way. Only after setting everything up I noticed that the users and dbs won't appear in whm/cpanel :) – jpeltoniemi Dec 23 '14 at 12:14

2 Answers2

4

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'));
Christopher Ellis
  • 1,106
  • 1
  • 8
  • 12
0

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)); 
Amar Pratap
  • 1,000
  • 7
  • 20