0

我正在尝试创建一个 mysql 用户并将其分配给创建的数据库。

我尝试将 $db_host 设置为 IP 地址、FQD、localhost(因为我在同一台服务器上运行)等。所有这些都没有成功。

关于我做错了什么有什么建议吗?(xmlapi.php 很好)

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

2 回答 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.

于 2013-04-05T22:37:40.930 回答
0

目前,cpanel 支持 cpanel json api .. 在这里您可以使用此代码 .. 这对我很有效

<?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;

?>
于 2020-09-04T10:19:45.557 回答