Scripts with PHP for CPanel

Posted on Jun 24, 2013
To automate an action in PHP for cpanel it's very simple. You just need to know some information about your CPanel.

$cpanel_user - the username that you use to log into your cpanel.
$cpanel_password - the password for cpanel.
$cpanel_host - your cpanel host. You can find this after you login to cpanel, right before 'http://' until ':'.
$cpanel_skin - cpanel skin. You can find this after '/frontend/', default is 'x'.


Simple HTML Form

You can use this html forms to make the scripts for cpanel, you can add your own fields that you need.
<form action="cpaneladd.php" method="POST">
<table>
<tr><td colspan="2" align="center">
<b>Cpanel info</b></td></tr><tr><td align="right">Cpanel host:
</td><td align="left">
<input type="text" name="cpanelhost"></td></tr>
<tr><td align="right">CPanel Username:</td><td align="left">
<input type="text" name="cpaneluser"></td></tr>
<tr><td align="right">CPanel Password:</td><td align="left">
<input type="text" name="cpanelpass"></td></tr>
<tr><td align="right">CPanel Skin:</td><td align="left">
<input type="text" name="cpanel_skin"></td></tr>
</table>
</form>


1. Adding a Database in CPanel

$newdb = "db_name";
$curl = curl_init();  //include CURL in PHP.ini
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user:
$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin
/sql/adddb.html?db=$new_db");

$result=curl_exec ($curl);
curl_close ($curl);


2. Adding a User to database in CPanel

$db_user = "db_user";
$db = "database";
$curl = curl_init(); //include CURL in PHP.ini
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user:
$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/
sql/addusertodb.html?user={$cpanel_user}_{$db_user}
&db={$cpanel_user}_{$db}&ALL=ALL");

$result=curl_exec ($curl);
curl_close ($curl);


3. Delete a database in CPanel

$db="db";
$curl = curl_init();  //include CURL in PHP.ini

curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user:
$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin
/sql/deldb.html?db=".$cpanel_user."_".$db);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($curl);
curl_close ($curl);


4. Delete a user in CPanel

$userdb="user_db";
$curl = curl_init();  //include CURL in PHP.ini

curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user:
$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/
sql/deluser.html?user=".$cpanel_user."_".$user_db);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result=curl_exec ($curl);
curl_close ($curl);


Good Tip for Create CPanel Database through PHP Script

Need to make sure that you are using a cpanel user/pass not root and also that you are using port 2083
require("xmlapi.php");
// downlaoded from https://github.com/CpanelInc/xmlapi-php/blob/master/xmlapi.php

$xmlapi = new xmlapi("Your CPanel Domain");   
$xmlapi->set_port(2083);   
$xmlapi->password_auth($opts['user'],$opts['pass']);    
$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false 

$cpaneluser=$opts['user'];
$db_name="something";
$db_user="else";
$db_pass=$opts['pass'];

//create database    
$createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($db_name));   

//create user 
$usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($db_user, $db_pass));   

//add user 
$addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb",
array("".$cpaneluser."_".$db_name."", "".$cpaneluser."_".$db_user."", 'all'));

I hope these tips have been helpful.

Leave a comment:

Thank you for your comment. After a while, our moderators will add it.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

© Twiwoo 2020 Cookie Policy