ayowande/cpanel-api

A lightweight PHP client for interacting with cPanel's API, providing methods for common tasks like managing domains, emails, and server resources.

v1.0.1 2024-08-24 15:51 UTC

This package is auto-updated.

Last update: 2025-04-24 17:42:21 UTC


README

A simple and lightweight PHP client for interacting with the cPanel API using cURL. This package allows you to easily manage cPanel accounts, domains, emails, and other server resources through a straightforward API.

Features

  • Simple and intuitive API for interacting with cPanel
  • Supports GET and POST requests to cPanel's API endpoints
  • Easy configuration via constructor parameters
  • Handles authentication with cPanel's API tokens

Installation

Install the package via Composer:

composer require ayowande/cpanel-api

Usage

Initialization

First, create an instance of the Cpanel class with your cPanel server details:

use ayowande\Cpanel\Cpanel;

$cpanel = new Cpanel(
    'example.com',    // cPanel server hostname
    'username',       // cPanel username
    2083,             // Port (usually 2083 for SSL)
    'your-api-token'  // cPanel API token
);

Sending a GET Request

To send a GET request to a specific cPanel API endpoint:

$response = $cpanel->get('/json-api/listaccts', [
    'api.version' => 1
]);

echo $response;

Sending a POST Request

To send a POST request with data:

$response = $cpanel->post('/json-api/createacct', [
    'api.version' => 1,
    'username' => 'newuser',
    'domain' => 'newdomain.com',
    'plan' => 'default'
]);

echo $response;

check out the test for more examples on various use cases

vendor/bin/phpunit

Handling Errors

Both get and post methods throw exceptions if the cURL request fails. You can handle these exceptions like this:

try {
    $response = $cpanel->get('/json-api/listaccts', [
        'api.version' => 1
    ]);

    echo $response;
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

Requirements

  • PHP 8.0 or higher
  • cURL extension enabled
  • cPanel API token

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss any changes.