Giter Club home page Giter Club logo

cpanel-uapi-php-class's Introduction

cPanel UAPI and API2 PHP class

GitHub release

PHP class to provide an easy-to-use interface with cPanel's UAPI and API2. Uses PHP magic functions to provide a simple and powerful interface.

v2.0 is not backwards compatible with v1.x, and will likley undergo a few more changes. See the changelog for details. The class has been renamed to cpanelAPI. Some more testing is required.

  • Note while this class is not depricated, there is a new Agnostic API class available which will do everything this class does (except 2FA) and works with any RESTful / HTTP API - basically anything thats not SOAP. https://github.com/N1ghteyes/apicore

Usage

If you choose to use this class, please Star it in Github. This gives me a better idea of the number of users and those effected when changes are made.

See the example files, but typical usage takes the form of:

Instantiate the class

$cPanel = new cpanelAPI('user', 'password', 'cpanel.example.com');

The API we want to use and the Module (also called Scope) are now protected and are set by __get().

The request layout looks like this: $cPanel->api->method->Module->request(args[])

The ->method part should be replaced with ->get for GET requests and ->post for POST requests, or omitted to default to GET requests.

As an example, suppose we want to use the UAPI to call the Mysql::get_server_information function:

$response = $cPanel->uapi->Mysql->get_server_information();
var_dump($response);

Now that we have set both the API and the Module, we can call other functions within this API and Module without specifying them again:

$response = $cPanel->create_database(['name' => $cPanel->user.'_MyDatabase']);
var_dump($response);

We can also change the Module scope without respecifying the API. Note that the Module call is case-sensitive.

$response = $cPanel->SSL->list_certs();

File upload example

$cPanel = new cpanelAPI($username, $password, $hostname);
$cPanel->uapi->post->Fileman
       ->upload_files(['dir' => REMOTE_PATH_RELATIVE_TO_HOME,
                       'file-1' => new CURLFile(LOCAL_PATH_TO_FILE)
                       ]);

API2

API2 is used in exactly the same way as the UAPI

$cPanel = new cpanelAPI('user', 'password', 'cpanel.example.com');

For example, suppose we want to use the API2 to add a subdomain:

$response = $cPanel->api2->SubDomain->addsubdomain(['rootdomain' => 'domain.com', 'domain' => 'sub']);
var_dump($response);

Two-Factor Authentication

To use this class on a cPanel instance with two-factor authentication (2FA), you need to pass the secret into the class constructor:

$cPanel = new cpanelAPI('user', 'password', 'cpanel.example.com', 'secret');

The secret can be found on the 2FA setup page. See Two-Factor Authentication for cPanel – Configure two-factor authentication for details.

cpanel-uapi-php-class's People

Contributors

deltik avatar n1ghteyes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpanel-uapi-php-class's Issues

no response

Hi , thanks for sharing this. When I try this class, I get no response and no errors .. have I missed something obvious ? I've included what I'm using below..

Thanks for any help.

// include class
include "includes/cpanel-UAPI-php-class/cpaneluapi.class.php"; //include the class file

// credentials
include_once("includes/cpanel_inc.php");;

// connect cpanel api
$capi = new cpanelAPI( $account, $passwd, $ip ); // instantiate the object

$response = $capi->uapi->Mysql->get_restrictions(); 
print_r($response);

$uapi->Fileman->upload_files reult You must specify at least one file to upload

While I run the code
$uapi= new cpanelAPI("user","pass","remote.server");
$response = $uapi->Fileman->upload_files(['dir'=>"public_html","file-1" => "/var/file.zip");

the $response looks like

(

  | [data] => stdClass Object
  | (
  | [failed] => 0
  | [warned] => 0
  | [uploads] => Array
  | (
  | )
  |  
  | [succeeded] => 0
  | )
  |  
  | [status] => 0
  | [errors] => Array
  | (
  | [0] => You must specify at least one file to upload.
  | )
  |  
  | [messages] =>
  | [metadata] => stdClass Object
  | (
  | )
  |  
  | )
`

how should we do to let the class upload files from the local server to the cpanel server.

No return from API

Hi, I'm having no return from Cpanel API and WHM 2FA is disable!

My test location file: https://sistemamobile.com.br/includes/hooks/teste.php
The teste.php code:

<?php                   
	echo "Cpanel API Test";

	require("mobileSetup/cpaneluapi.class.php"); 
			
	echo "<br>Include, ok";


	//testing...

	
	$CPUSER = "{user}";
	$CPPASS = "{pass}";
	$CPURL = "96.47.227.212"; //96.47.227.212
	
	$cPanel = new cpanelUAPI($CPUSER, $CPPASS, $CPURL); //instantiate the object 'cPanel.example.com'
	$response = $cPanel->uapi->Mysql->get_restrictions(); 
	print_r($response);
		
?>

is it possible to delete a database using the uapi?

Hi,

this is definitely a great class to extended the api library and make the job easier.
I have successfully used and i can able to create database and set privilages to it.

Now i am looking for the delete option to remove the database. Is it possible to remove database from the uapi ?

I'm getting no response

I'm trying to get domain info, it is working good but for some accounts it is neither giving response nor throwing error. I checked credentials are valid but API giving no response.
It is very hard to identify cause of issue without response. Please help, Thank you.

 try {
            $cPanel = new cpanelAPI($this->username, $this->password, $this->url);
            $response = $cPanel->uapi->DomainInfo->single_domain_data(array('domain' => $this->url));
            print_r($response);die;
        } catch(Exception $e) {
            print_r($e->getMessage());die;
            throw new Exception($e->getMessage());
        }

is it possible to login to cPanel interface?

Hello,

Is it possible to login to cpanel interface to implement similar to single sign-on using this class when you dont have access to whm (only cPanel username & password)?

Thanks

Sugestion about __call method

On line 55 you could change
from:
return json_decode($this->APIcall($name, $arguments[0]));
to:
return json_decode($this->APIcall($name, $arguments[0]), true);
That will return a full array of arrays instead of a object of arrays and objects.

I changed on my local file.

Third param doesn't accept array

Hi, I noticed when trying to use install_ssl using the uapi, which requires the third param to be an array (rather than another string) the library code errors out.

Can't quite figure out how to adapt the curl request to format an array of items.

Also no response

I am having the same no response problem as issue #5 but I am using https and not using 2Fa.
I am get no output.

include "include/cpaneluapi.class.php"; //include the class file
// credentials
include_once('include/cpanel_inc.php');
// connect cpanel api
$capi = new cpanelAPI( $account, $passwd, $ip, $secret ); // instantiate the object
//no SSL
// $capi->port = 2082; //Check this!
// $capi->ssl = FALSE;
//get response
$response = $capi->uapi->Mysql->get_restrictions();
print_r($capi->getLastRequest());

If I uncomment the 2 lines that disable SSL I get the following output, "http://xxxxxxxxxxxxx:2082/execute/Mysql/get_restrictions?"
When logging in manually I use, https://xxxxxxxxxxxx:2083.

Create Cronjob

Hi,

The script is excellent! I can create a MySQL db and a username to it.

I also would like to create a cronjob. It is possible?

Thanks,
Francisco

Encoding Url

Hi;

In some cases, for example for the cron module (Api v2) the command may contains white spaces. Example:

$action = array(
'command' => 'php -c /home/xxx/php.ini /home/xxx/public_html/app/cron/instance/backup.php',
'day' => '',
'hour' => '18',
'minute' => '0',
'month' => '
',
'weekday' => '*',
);
//$cron will contains the response in an array (not an object)
//so the mix of these functions: json_decode(json_encode($OBJECT), true)
//do the magic :D
$cron = json_decode(json_encode($cpapi2->api2->Cron->add_line($action)), true);
print_r($cron);

So the code of the method "APIcall" should be:

protected function APIcall($name, $arguments){ .... foreach ($arguments as $key => $value) { $this->requestUrl .= "&" . $key . "=" . urlencode($value); } ... }

Cannot access protected property cpanelAPI::$scope

I can't seem to find helpful info on this error - hope someone can help.

The troublesome line is $uapi->scope = $username.'_Mysql'; where $username is my cpanel username.

I also had to change the example instantiation line from
$uapi = new cpanelUAPI( to
$uapi = new cpanelAPI( because there is no cpanelUAPI class in cpaneluapi.class.php

any ideas?

Backup files to an other server

Hi!

I have an array of my WHM websites (host, user, pass), i need to host a script in my root server, that reads the array and send the generated backups to another FTP Server.

I used to do this with "xmlapi", and access with Cron, but my host updated cPanel so it stopped running.

Is it possible with UAPI and this class? Could you help to build it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.