0

I am trying to create a mysql user and assign it to the created database.

I have tried setting $db_host as IP address, FQD, localhost (since I am running from the same server) etc. All of these has no success.

Any advice on what I'm doing wrong? (xmlapi.php is being incuded fine)

include("xmlapi.php");
$db_host = "usingfullyqualifieddomain";
$cpuser = "myuser";
$cppass = "mypass";

$xmlapi = new xmlapi($db_host);
$xmlapi->set_port(2083);
$xmlapi->password_auth($cpuser,$cppass);
$xmlapi->set_debug(1);
//create database
print $xmlapi->api1_query($cpuser, "Mysql", "adddb", 'myDatabaseName');
//create user
print $xmlapi->api1_query($cpuser, "Mysql", "adduser", array('user' => 'myDBUser','pass'=>'myDBPwd')); 
1
  • Did you get solution for it? Commented Dec 23, 2015 at 1:33

2 Answers 2

2

The error is probably related to the way you're making your first service call, to create the database. Here is how I do it, on CPanel 11:

$auth_user = 'XXXXXX'; 
$auth_pass = 'XXXXX'; 
$server = 'XXXXXXXX.com';
$json_client = new \xmlapi($server);

$json_client->set_output('json'); 
$json_client->set_port(2083); 
$json_client->password_auth($auth_user, $auth_pass);
$json_client->set_debug(1);

# Create Database
$result = $json_client->api1_query( $auth_user, 'Mysql', 'adddb', array($shop->alias));
var_dump($result);

Note that the fourth parameter should be an array with the parameters, and not a string as you are doing it.

Also notice that the print on the api call result is not very useful for debugging. Try var_dump'ing it, as it will show you some more interesting information to work on.

Sign up to request clarification or add additional context in comments.

1 Comment

api 1 is obsolete. please use api2
1

currently, cpanel support the cpanel json api.. here you can use this code .. this worked for me well

<?php 

$cpanelusername = "cpanelusername";
$cpanelpassword = "cpanelpassword";  
$domain = 'mydomain.com';
   
$query = "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_module=Mysql&cpanel_jsonapi_func=adddb&cpanel_jsonapi_apiversion=1&arg-0=DBNAME";

$curl = curl_init();                                // Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);       // Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);       // Allow certs that do not match the hostname
curl_setopt($curl, CURLOPT_HEADER,0);               // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);       // Return contents of transfer on curl_exec
$header[0] = "Authorization: Basic " . base64_encode($cpanelusername.":".$cpanelpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);    // set the username and password
curl_setopt($curl, CURLOPT_URL, $query);            // execute the query
$result = curl_exec($curl);
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");   
                                                    // log error if curl exec fails
}
curl_close($curl);

print $result;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.