Create database dynamically using php code?

ragu

Registered
Sep 23, 2019
2
0
1
Erode,Tamilnadu
cPanel Access Level
Website Owner
how used
Code:
   $server_login_link = '******';
    $whmusername = '******';
    $hash = '******';
    $query = "[URL]https://example.com:2087/cpsesss_HJXX,TTX8H/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=createdb&db=example_database[/URL]";

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

    $header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$hash);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
    curl_setopt($curl, CURLOPT_URL, $query);

    $result = curl_exec($curl);
    print_r(json_decode($result));
my cpanel version 82.0.15



the result will

Code:
// stdClass Object ( [cpanelresult] => stdClass Object ( [apiversion] => 2 [error] => Access denied [data] => stdClass Object ( [reason] => Access denied [result] => 0 ) [type] => text ) )

please replay me what i change the code
 
Last edited by a moderator:

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
97
50
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
In your $query, you need to clean out the forum templating code for a URL at the start and at the end, as well as remove the cpsess. Your cpanel user needs to actually be assigned to a cpanel user. I recommend using UAPI (version 3) Mysql::create_database instead of the API 2 version.

In the end, your $query should look something like...

PHP:
$query = "https://example.com:2087/json-api/cpanel?cpanel_jsonapi_user=$user&cpanel_jsonapi_apiversion=3&cpanel_jsonapi_module=Mysql&cpanel_jsonapi_func=create_database&name=example_database";
where '$user' is a variable you will need to determine and assign before the query.

Additionally, access hashes are deprecated as of cPanel & WHM version 64. We recommend you use API Tokens for API authentication.
 
  • Like
Reactions: cPanelMichael