Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cPanel LiveAPI PHP Class on Siteground shared hosting account

I was wondering if anyone has implemented a PHP application level access to cPanel on a shared hosting provider ( in my case - Siteground). I've been looking into the documentation on the LiveAPI PHP website and it mentions that it involves managing some files in the main cPanel installation directory. I couldn't find references to any downloadable resources, so if could provide links to those and an example of how you carried out your implementation, that would be great.

I wish to programmatically (in PHP) create subdomains in cPanel and provide the respective routing directories for the same.

I found this related question but it lead to a dead end as the main PHP class link is not working

https://stackoverflow.com/questions/7549015/php-create-subdomain-over-cpanel-api

like image 973
Prathamesh Datar Avatar asked Jan 13 '23 07:01

Prathamesh Datar


2 Answers

Citizen Kepler's links are now dead, and the XMLAPI at github here is deprecated.

However, combining the codes given here for authentication and here for adding a subdomain, gives us the following script which seems to work just fine on shared hosting:

<?php 

$cpanelusername = "example";
$cpanelpassword = "**********";
$subdomain = 'newsubdomain';
$domain = 'example.com';
$directory = "/public_html/$subdomain";  // A valid directory path, relative to the user's home directory. Or you can use "/$subdomain" depending on how you want to structure your directory tree for all the subdomains.

$query = "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=$subdomain&rootdomain=$domain&dir=$directory";

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

?>

The result should be something like this:

{"cpanelresult":{"func":"addsubdomain","event":{"result":1},"apiversion":2,"module":"SubDomain","data":[{"reason":"The subdomain “newsubdomain.example.com” has been added.","result":1}],"preevent":{"result":1},"postevent":{"result":1}}}

Then to delete the subdomain run the same script, but using this query:

$deletesub =  "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_func=delsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=".$subdomain.'.'.$domain."&dir=$directory";  //Note: To delete the subdomain of an addon domain, separate the subdomain with an underscore (_) instead of a dot (.). For example, use the following format: subdomain_addondomain.tld

And to remove the directory (including all its contents), run this:

$deletedir =  "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_func=fileop&op=unlink&sourcefiles=$directory";
like image 190
Noel Swanson Avatar answered Jan 28 '23 10:01

Noel Swanson


I believe that you are not looking fte the LiveAPI since the LiveAPI is for developing inside cPAnel/WHM. The LiveAPI is for creating plugins inside the cPanel and WHM interfaces.

If you are looking to add a subdomain to your account, the JSON/XML API's are much more suited to your task. If possible use the JSON api since cPanel Docs cite it as the preferred API due to it being faster than the XML api. To add a subdomain using the JSON/XML APIs you would use the following API call:

XML:

https://domain.tld:2083/xml-api/cpanel?cpanel_xmlapi_func=addsubdomain&cpanel_xmlapi_module=SubDomain&cpanel_xmlapi_version=2&domain=sub&rootdomain=maindomain.tld

JSON:

https://domain.tld:2083/json-api/cpanel?cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=sub&rootdomain=maindomain.tld

In the above string the arguments that you will need to modify are:

  • domain (string) - The local part of the subdomain you wish to add. (e.g. 'sub' if the subdomain's is sub.example.com) This value should not include the domain with which the subdomain is associated.
  • rootdomain (string) - The domain to which you wish to add the subdomain.

Below is further documentation including how to integrate these API commands into your php scripts and how to authorize for the API.

  • http://docs.cpanel.net/twiki/bin/vief/ApiDocs/Api2/ApiSubDomain#SubDomain::addsubdomain
  • http://docs.cpanel.net/twiki/bin/view/SoftwareDevelopmentKit/CallingAPIFunctions
  • http://docs.cpanel.net/twiki/bin/view/SoftwareDevelopmentKit/XmlApi
  • http://docs.cpanel.net/twiki/bin/view/SoftwareDevelopmentKit/ApiAuthentication
like image 40
Citizen Kepler Avatar answered Jan 28 '23 11:01

Citizen Kepler