Now, this may not be a problem that many people face but getting good sample code from the cPanel XML-API documentation is a bust. They do have a nice Forum though, where it is possible to get the answers one may seek but forums involve a lot of reading which I try to avoid when looking for what should be quick solutions.
I have therefore decided to document something that I figured would only take me a couple of minutes to figure out, only for it to kill four precious hours. So here goes…
PHP script to programmatically create an add-on domain in cPanel.
First, you will need to download cPanel’s XML-API for PHP which you can find in the links below.
Github – https://github.com/CpanelInc/xmlapi-php/archive/master.zip (original download from cPanel Inc.)
ajagwe.com Link – cPanel PHP XML-API (includes file with changes below)
We will be using an API2 function to add an add-on domain and we will need to parse some data to the function. So let’s look at the example code in api2_example_withargs.php that is included with the above download:
$ip = ‘127.0.0.1’;
$root_pass = ‘password’;
$account = ‘someuser’;
$email_account = ‘randomemail’;
$email_domain = ‘somedomain.com’;
$xmlapi = new xmlapi($ip);
print $xmlapi->api2_query($account, ‘Email’, ‘getdiskusage’, array(domain=>$email_domain, login=>$email_account) );
In the above example…
- the IP is the IP address of your server. (You could use a hostname here, but using an IP gives you a slight performance improvement.)
- $root_pass stores the password you want to login to the XML API with. If you are logging in as a cPanel user, you can store the cPanel user’s password in this variable.
- $account is the cPanel user you want to perform this action on.
Many people use the API as a reseller or root user, but you can login as a cPanel user. So if you’re not logging into the API as the root user, just replace:
Now to the heart of the matter.
With these API2 function calls, the first parameter is expected to be the cPanel account we want to perform this action upon, the second parameter is the Module, the third parameter the function to call and the fourth parameter is an array of input arguments for the API2 function.
To create an add-on domain, we will use an API2 function, and this function will take four parameters as described earlier. With this in mind, replace the above code
$xmlapi->api2_query($account, "Email", "getdiskusage", array(domain=>$email_domain, login=>$email_account) );
$xmlapi->api2_query($account, "AddonDomain", "addaddondomain", array(newdomain=>"example.com", dir="public_html/example", subdomain=>"example", pass=>"ftpPass") );
This will create an add-on domain example.com on the account, serving content out of public_html/example with a FTP user of example with password ftpPass being created as part of this API call.
Just like in the cPanel interface, creating the FTP user/pass is required. Also, just like cPanel, you can have the addon domain serve content from anywhere within that cPanel account.
Now find below a link with to the XML-API for PHP that includes the file api2_example_addondomain.php with all the above changes.
ajagwe.com Link – cPanel PHP XML-API
Stand by for more examples to be included into the above download as I continue to tackle more cPanel related issues that are bound to come my way.
It has come to my attention that there is a possibility of getting ACCESS DENIED error when trying the above solution. This may possibly be due to the fact that unless stated otherwise, the API assumes you are logging in with root/reseller credentials on port 2087. Should your WHM or cPanel be on a different port – please add the line of code below to your script to specify which port the API should use
Replace xxxx with the port number your cPanel or WHM is configured to use. Place the above code before the