eliashaeussler/cpanel-requests

Small PHP library enabling requests to cPanel instances

3.1.1 2024-01-16 19:13 UTC

README

Coverage Maintainability Tests CGL Latest Stable Version Total Downloads License

📦 Packagist | :floppy_disk: Repository | :bug: Issue tracker

A simple PHP project to make API requests on your cPanel installation. This allows you to call modules inside the installation and interact with them to add, show or list data such as domains, e-mail accounts, databases and so on.

The project makes use of UAPI. Therefore, it is required to have a cPanel installation with at least version 42 running.

🔥 Installation

composer require eliashaeussler/cpanel-requests

⚠️ If you want to use two-factor authentication together with the HTTP session authorization method, you must manually require the spomky-labs/otphp package (>= 11.1).

⚡Usage

Authorization

The following authorization methods are currently available:

Type Implementation class
Authorization via API token (recommended) Application\Authorization\TokenAuthorization
Authorization via HTTP session Application\Authorization\HttpAuthorization

💡 You can also provide your own implementation for authorization at your cPanel instance. For this, you have to implement the interface Application\Authorization\AuthorizationInterface.

Create a new CPanel instance

Once you have selected an authentication method, you can create a new Application\CPanel instance:

use EliasHaeussler\CpanelRequests\Application;

/** @var Application\Authorization\AuthorizationInterface $authorization */
$cPanel = new Application\CPanel($authorization, 'example.com', 2083);

Perform API requests

Now you're able to make API requests:

use EliasHaeussler\CpanelRequests\Application;

/** @var Application\CPanel $cPanel */
$response = $cPanel->api('<module>', '<function>', ['optional' => 'parameters']);
if ($response->isValid()) {
    // Do anything...
    // Response data can be fetched using $response->getData()
}

Note that currently only GET requests are supported.

Visit the official documentation to get an overview about available API modules and functions.

🐝 Example

use EliasHaeussler\CpanelRequests\Application;
use EliasHaeussler\CpanelRequests\Http;

$authorization = new Application\Authorization\TokenAuthorization(
    username: 'bob',
    token: '9CKU401OH5WVDGSAVXN3UMLT8BJ5IY',
);
$cPanel = new Application\CPanel(
    authorization: $authorization,
    host: 'cpanel.bobs.site',
    port: 2083,
    protocol: Http\Protocol::Https,
);

// Fetch domains from cPanel API
$response = $cPanel->api(
    module: 'DomainInfo',
    function: 'list_domains',
);

if (!$response->isValid()) {
    throw new \RuntimeException('Got invalid response from cPanel application.');
}

$domains = $response->getData()->data;
echo 'Bob\'s main domain is: ' . $domains->main_domain;

🗑️ Cleanup

The project provides a console application that can be used to execute several cleanup commands from the command line.

# General usage
vendor/bin/cpanel-requests

# Remove expired request cookie files (default lifetime: 1 hour)
vendor/bin/cpanel-requests cleanup:cookies
vendor/bin/cpanel-requests cleanup:cookies --lifetime 1800

# Remove log files
vendor/bin/cpanel-requests cleanup:logs

🧑‍💻 Contributing

Please have a look at CONTRIBUTING.md.

⭐ License

This project is licensed under GNU General Public License 3.0 (or later).