Connecting to cPanel API from remote server

Neutrall

Active Member
PartnerNOC
Jul 22, 2014
27
3
53
cPanel Access Level
DataCenter Provider
Hi all,

I would need some help on initiating a call to cPanel API from a remote server.

I’ll need to access the API for getting a listing and modifying subdomains and reading and modifying a file on the hosting.

Currently I have some problem on how to create the actual API call in PHP, this is what I have so far :

Hi all,

I would need some help on initiating a call to cPanel API from a remote server.

I’ll need to access the API for getting a listing and modifying subdomains and reading and modifying a file on the hosting.

Currently I have some problem on how to create the actual API call in PHP, this is what I have so far :

Code:
$user = "user";
$pass = "password";
// The URL
$url = 'https://'.$user.':'.$pass'@mycpanel.com:2083/cpsess###########/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=listsubdomains&regex=domain';


// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 
// Get response from the server.
$resp = curl_exec($ch);

// Unserialize data
$res = unserialize($resp);
Currently, I don’t understand with what I should change the “cpsess###########” with!

Secondly, I’m not even sure I’m using the right authentication method!

And Finally, I see that is I need to modify the content of one file, this need to be done with UAPI, but can I access UAPI from a remote server?

Thank you!
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463

Andy M-B

Member
Jul 30, 2013
10
1
3
cPanel Access Level
Root Administrator
Hello,

The following documents are helpful when first starting out with our API:

Guide to API Authentication - Software Development Kit - cPanel Documentation
Guide to UAPI - Software Development Kit - cPanel Documentation

You may also find the examples in the PHP Client Class for cPanel helpful:

GitHub - CpanelInc/xmlapi-php: A PHP Class for Interacting with cPanel's XML-API

Thank you.
Hi Michael, I see that xmlapi-php hasn't been updated in four years: Is it still current? Thanks.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
Hello @Andy M-B,

While that class may still work, it's a good idea to use JSON instead of the XML-like output of our APIs when developing a script. See the following quote from our March 2017 Development Update Blog:

  • XML-like output of APIs
    The XML-like output of our API (often called the XML API) is already officially deprecated, and cPanel & WHM Version 70 will be the last version to support this output. Integrators and API users will want to begin switching to use the JSON output of the API now. The XML-like format has long caused problems for integrators and developers because it is not valid XML, and we find that removing it completely will help reduce that.
Thank you.