Get host information UAPI

Alex Nix

Member
Nov 25, 2022
6
0
1
UK
cPanel Access Level
Website Owner

Alex Nix

Member
Nov 25, 2022
6
0
1
UK
cPanel Access Level
Website Owner
@cPRex

What I need to use for connecting to cPanel with email and password from your documentation?

 

rbairwell

Well-Known Member
May 28, 2022
108
47
28
Mansfield, Nottingham, UK
cPanel Access Level
Root Administrator
I think you are doing the authentication incorrectly.

The new/current way for connecting to the API is to use cPanel API tokens. For cPanel user accounts you want Security->Manage API Tokens: and for WHM/root level you want Development->Manage API tokens. If you want to prompt for the username and password, you can then call Create cPanel API Token with the Basic authentication headers.

You then make calls using the user's username and the API Token as the password - see Using an API Token ion an "Authorization: cpanel " header.
 

Alex Nix

Member
Nov 25, 2022
6
0
1
UK
cPanel Access Level
Website Owner
rbairwell Do you have some messanger or somethink else?

It doesn't work...((

require_once "/usr/local/cpanel/php/cpanel.php";

$cPusername = $details['service']['username'];
$cPpassword = $details['service']['password'];
$cPhost = $details['server']['host'];
$cPdomain = $details['service']['domain'];
$categoryId = $details['product']['category_id'];

// Print the header
header('Content-Type: text/plain');

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

// Call the API
$response = $cpanel->uapi(
'Tokens',
'create_full_access',
array (
'name' => $cPusername,
)
);

$data = "<pre>".print_r($cpanel, true)."</pre>";
file_put_contents('cPanel_filename.txt', $data);
 
Last edited:

rbairwell

Well-Known Member
May 28, 2022
108
47
28
Mansfield, Nottingham, UK
cPanel Access Level
Root Administrator
Hi,

That code example is a mangled version of the "LiveAPI PHP" code from the Create Full Access page : if you read the instructions in the comments at the top of that example, it'll tell you how to use that example version.

I advise for now just creating the token via cPanel/WHM manually and then:
* If you are running this page within cPanel, follow the "LiveAPI PHP" examples on the page (see also "Guide to the LiveAPI System - PHP Class")
* If you are running this page outside of cPanel, you'll need to make calls using something like curl (see "Using An API Token")

If you are using curl and want to get the MySQL server information, then the URL needed (according to that page https://hostname.example.com:2083/cpsess##########/execute/Mysql/get_server_information (which is a cPanel session based URL) and if we change that to a UAPI API call with the username username and the API Token U7HMR63FHY282DQZ4H5BIH16JLYSO01M for host example.com , you'll get the command line request:


Code:
curl -H'Authorization: cpanel username:U7HMR63FHY282DQZ4H5BIH16JLYSO01M' 'https://example.com:2083/execute/Mysql/get_server_information'
You might find the PHP example in Guide to API Authentication - API Tokens in WHM a bit of a help.

I am available on a consultancy basis, but it would be chargeable work at 75GBP/ph. Posting on this forum is my way of "giving back to the community" for helping me many many years ago (and still to this do on odd bits an pieces) - one to one work (especially in private) is different. I hope you understand.
 

Alex Nix

Member
Nov 25, 2022
6
0
1
UK
cPanel Access Level
Website Owner
rbairwell ok but how can I upload my file using you API now (php)?

My code is:

// Define the API call.

$file = 'my_file.php';
$cpanel_host = $this->cPdomain;
$request_uri = "http://$cpanel_host:2083/execute/Fileman/upload_files";

$destination_dir = "public_html";
$payload = array(
'dir' => $destination_dir,
'file-1' => $file
);

// Set up the curl request object.
$ch = curl_init( $request_uri );

curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $ch, CURLOPT_USERPWD, $this->cPusername . ':' . $this->cPpassword );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

// Set up a POST request with the payload.
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// Make the call, and then terminate the curl caller object.
$curl_response = curl_exec( $ch );
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close( $ch );

But it doesn't work as well...((
 

rbairwell

Well-Known Member
May 28, 2022
108
47
28
Mansfield, Nottingham, UK
cPanel Access Level
Root Administrator
In that tutorial, instead of using the username and password and the
CURLOPT_USERPWD lines as detailed in that, use the token and the header setting as per Guide to API Authentication - API Tokens in WHM - using port 2083 and "Authorization: cpanel" instead of port 2087 and "Authorization: whm"

But it doesn't work as well...((
In what way? And are you trying to run the script on the server itself or "remotely"?