Using cpanel UAPI in from my php

theveil

Registered
May 15, 2020
3
0
1
India
cPanel Access Level
Website Owner
Hi,

I've been searching for a way to create a subdomain from my php file possibly using a cpanel api. I'm sorry icould not locate a UAPI php library or the UAPI function in the cpanel documentation. Also I tried to use the method described in the below post
https://www.codepunker.com/blog/usi...-and-email-accounts-on-a-cpanel-driven-server

using github php library
https://github.com/mgufrone/cpanel-whm

My problem when using the above library is that I'm getting access denied error. I've correctly given my cpanel username & password but still getting the error.

Could you please help me find a resolution. If the above library is outdated, could you redirect me to the latest UAPI usage example through PHP.

I'm using shared hosting.

Error as below: (example used for creating a database)

Client error: `POST https://domain.com:2083/json-api/cp...=create_database&cpanel_jsonapi_user=username` resulted in a `403 Forbidden Access denied` response: {"cpanelresult":{"apiversion":"2","error":"Access denied","data":{"reason":"Access denied","result":"0"},"type":"text"}} (truncated...)
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,295
1,255
313
Houston

theveil

Registered
May 15, 2020
3
0
1
India
cPanel Access Level
Website Owner
Thanks for the reply. Can this function directly declared in the php file without any libraries.
Also could you plesse elaborate the first line as mention in the documentation

Code:
$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
Do we need to provide the connection details in the CPANEL() function eg: server, username, password

Documentation doesn't mention this specifically. Sorry, I'm novice into using UAPI.
 

theveil

Registered
May 15, 2020
3
0
1
India
cPanel Access Level
Website Owner
Hey @cPanelTJ ,
Thanks for turning up.
I have two question for you. I feel my previous question was not well answered.
  1. Regarding the usage and declaration of the UAPI function directly in the php file. I'm still not clear on how this is achieved.
    Documentation do not state how the function connects to the cpanel (server, username,password etc...).
    I have tried copy paste the function as given in the documentation as there is nothing needed to customise othere than providing subdomain name and domain name. It didn't work.
  2. Regarding LiveAPI.
    Code:
    // Instantiate the CPANEL object.require_once "/usr/local/cpanel/php/cpanel.php";
    I could not locate the file cpanel.php in my cpanel directory in the server.Can I still use the function?
    Also /usr/local/cpanel/php/cpanel.php , Is this the actual file path or should i need to check and make changes if necessary.

From the inset what I'm trying is to execute is subdomain creation automatically from my php script without me manually login to cpanel.
Further help is greatly appreciated.
 
Last edited:

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
80
34
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
If you are running locally and therefore using LiveAPI PHP, then the API will be run for the account that executes the file. Server and username at that point do not need to be explicitly defined. I am assuming from the information you provided that you are a cPanel-level user, but let me know if you aren't.

The LiveAPI PHP would look something like:
PHP:
<?php
include("/usr/local/cpanel/php/cpanel.php");

$cpanel = new CPANEL(); // Connect to cPanel - only do this once.

//Retrieve user settings for example.com.
$variables = $cpanel->uapi(
    'Subdomain', 'addsubdomain',
    array(
        'domain' => 'mysubdomain',
        'rootdomain' => 'mydomain.com',
        )
);

?>
 
Oct 10, 2017
7
0
1
Netherlands
cPanel Access Level
Root Administrator
I have the same problem.

Even this doesn't work :

Code:
<?php

if ( file_exists("/usr/local/cpanel/php/cpanel.php") ) {
  require_once "/usr/local/cpanel/php/cpanel.php";
} else {
   echo  'Cannot find file cpanel.php';
}
Even the echo line did'n't process, just a blank page.
It looks like the require once command cannot access the file.

Should we place the cpanel.php in an other directory?

The lowest directory I can access is the "/home/<myaccount>" directory

Any help would be very appreciated.
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
80
34
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Hi @theveil and @Marcel Horsthuis

Are you following the guidelines of the LiveAPI system? (source: https://documentation.cpanel.net/display/DD/Guide+to+the+LiveAPI+System)

Applications that use a LiveAPI environment must meet the following requirements:
  • The code must include the appropriate environment and instantiate the respective class's object.
  • File names must end with the appropriate file extension:
    • PHP — .livephp or .live.php
    • Perl — .livepl or .live.pl
    • Ruby — .liverb or .live.rb
    • Python — .livepy or .live.py
    • CGI — .livecgi or .live.cgi
  • Files, or symlinks to the relevant files, must exist in the /usr/local/cpanel/base/frontend/theme/ directory, where theme is the cPanel theme (paper_lantern or another valid theme on the server).
 
Oct 10, 2017
7
0
1
Netherlands
cPanel Access Level
Root Administrator
Hi @cPanelIT

Thank you for pointing to this part of the documentation.
I have done the steps described in the documentation.

I made a test.live.php file and located it in the /usr/local/cpanel/base/frontend/paper_lantern/ directory

How to link to this file in a php file in the public_html directory or via an url.

Thanks in advance.
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
80
34
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
I'm not sure if linking from public_html, but in general the full path is: https://<hostname>:2083/cpsess<session_numbers>/frontend/paper_lantern/<directory>/<file>.live.php.

Most links within cPanel just use /frontend/paper_lantern/<directory>/<file>.live.php.