
Originally Posted by
hostb
I appreciate very much your reply.
But I need a little more info. A newbie unfortunately. By background is in C.
This class seems to contain only the WHM API's.
Are you suggesting I modify it to add the cPanel calls I want to use.
ie the addaddondomain API.
There are many APIs when dealing with cPanel/WHM, but there are 3 main APIs - all of which are covered by the PHP class Matt made. PHP has a very similar (though much simpler) syntax to C.
WHM functions are primarily handled by the XML API. Many of these functions already have their own function calls built into the PHP class.
cPanel functions are handled by API1 and API2. None of these functions are already built into the PHP class, but the framework for calling these functions is part of the PHP class.
API1 functions look like Module="function(arguments)" whereas API2 functions look like Module::function(arguments). Keep this in mind as you move forward with possibly automating more tasks with your scripting.
Much confusion comes from the fact that you can call API1 and API2 functions via the XML API. In this case, you can just think of the XML API as being a bridge connecting your custom script that is outside the cPanel interface into the cPanel APIs.
We're using an API2 function to add an addon domain, so let's look at the example code in api2_example_withargs.php that was included with the PHP 5 class so we can get started:
PHP Code:
include("xmlapi.php.inc");
$ip = "127.0.0.1";
$root_pass = "somepassword";
$account = "someuser";
$email_account = "someemail";
$email_domain = "somedomain.test";
$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("root",$root_pass);
$xmlapi->set_debug(1);
print $xmlapi->api2_query($account, "Email", "getdiskusage", array(domain=>$email_domain, login=>$email_account) );
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:
PHP Code:
$xmlapi->password_auth("root",$root_pass);
With
PHP Code:
$xmlapi->password_auth($account,$root_pass);
Next, we're replacing:
PHP Code:
$xmlapi->api2_query($account, "Email", "getdiskusage", array(domain=>$email_domain, login=>$email_account) );
with your API2 function call. The first parameter is going to be the cPanel account we perform this action upon, the second parameter is the Module, third parameter the function to call and the fourth parameter is an array of input arguments for the API2 function.
In the PHP class, we used an associative array (similar to a hash table in other languages) for API2 functions and a regular (sequential) array for API1 functions. This is because arguments in API2 are named, whereas in API1 it's all based on the order in which the parameters are passed.
We are using API2, and this function takes 4 parameters as I described in my prior post.
All of this in mind, we replace the above code with:
PHP Code:
$xmlapi->api2_query($account, "AddonDomain", "addaddondomain", array(newdomain=>"example.com", dir="public_html/example", subdomain=>"example", pass=>"ftpPass") );
This will create an addon 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, just just folders within public_html.
This is admittedly a crash course in calling API2 via the PHP class that Matt has created, so if you need clarification or encounter any errors, please let me know.