PHP code example of chuckbartowski / cpanel-sdk

1. Go to this page and download the library: Download chuckbartowski/cpanel-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chuckbartowski / cpanel-sdk example snippets


$cpanel->email()->create('support', 'example.com', 'S3cure!Pass', quotaMb: 250);
$whm->accounts()->create('customer1', 'customer1.com', ['plan' => 'starter']);
$whm->accounts()->createUserSession('customer1');

use ChuckBartowski\CpanelSdk\Client\CpanelClient;
use ChuckBartowski\CpanelSdk\Client\WhmClient;
use ChuckBartowski\CpanelSdk\Cpanel;
use ChuckBartowski\CpanelSdk\Whm;

$cpanel = new Cpanel(new CpanelClient(
    host: 'server.example.com',
    username: 'myaccount',
    token: getenv('CPANEL_API_TOKEN'),
    port: 2083,
));

$cpanel->email()->create('support', 'example.com', 'S3cure!Pass', quotaMb: 250);
$cpanel->dns()->addRecord('example.com', 'www', 'A', '203.0.113.10');

$whm = new Whm(new WhmClient(
    host: 'server.example.com',
    username: 'root',
    token: getenv('WHM_API_TOKEN'),
    port: 2087,
));

$whm->accounts()->create('customer1', 'customer1.com', ['plan' => 'starter']);

new CpanelClient(
    string $host,
    string $username,
    string $token,
    int $port,                            // 2083 for cPanel, 2087 for WHM
    bool $verifySsl = true,
    float $timeout = 30.0,
    ?HttpClientInterface $httpClient = null,  // inject your own (retries, proxy, mock…)
);

// config/bundles.php
return [
    ChuckBartowski\CpanelSdk\CpanelSdkBundle::class => ['all' => true],
];

use ChuckBartowski\CpanelSdk\Cpanel;

final class MailboxProvisioner
{
    public function __construct(private readonly Cpanel $cpanel)
    {
    }

    public function provision(string $localPart, string $domain, string $password): void
    {
        $this->cpanel->email()->create($localPart, $domain, $password, quotaMb: 512);
    }
}

use ChuckBartowski\CpanelSdk\Whm;

final class HostingAccountManager
{
    public function __construct(private readonly Whm $whm)
    {
    }

    public function open(string $username, string $domain): void
    {
        $this->whm->accounts()->create($username, $domain, [
            'plan' => 'starter',
            'contactemail' => '[email protected]',
        ]);
    }

    public function suspendForNonPayment(string $username): void
    {
        $this->whm->accounts()->suspend($username, 'unpaid invoice');
    }
}

$cpanel->client()->uapi('Batch', 'strict', ['command' => $commands], 'POST');
$cpanel->client()->api2('Cron', 'listcron');

$whm->client()->call('sethostname', ['hostname' => 'srv2.example.com'], 'POST');
$whm->client()->cpanelUapi('customer1', 'Email', 'list_pops');

$cpanel->email()->accounts('example.com');
$cpanel->email()->create('support', 'example.com', 'S3cure!Pass', quotaMb: 250);
$cpanel->email()->addForwarder('example.com', 'contact', '[email protected]');

$cpanel->domains()->addSubdomain('api', 'example.com', 'public_html/api');

$cpanel->mysql()->createDatabase('myaccount_app');
$cpanel->mysql()->createUser('myaccount_app', 'S3cret!');
$cpanel->mysql()->grant('myaccount_app', 'myaccount_app');

$cpanel->ftp()->create('deploy', 'S3cret!', homeDir: 'public_html', quotaMb: 0);
$cpanel->ftp()->delete('deploy', destroyHomeDir: false);

$cpanel->ssl()->install('example.com', $certificatePem, $keyPem, $caBundlePem);

$cpanel->files()->write('public_html', '.htaccess', $rules);
$cpanel->files()->extract('backup.tar.gz', 'public_html');
$cpanel->files()->chmod('public_html/config.php', '0600');

$cpanel->dns()->records('example.com', ['type' => 'A']);
$cpanel->dns()->addRecord('example.com', 'www', 'A', '203.0.113.10', ttl: 3600);
$cpanel->dns()->editRecord('example.com', line: 22, params: ['address' => '203.0.113.11']);
$cpanel->dns()->removeRecord('example.com', line: 22);

$session = $whm->accounts()->createUserSession('customer1');
$redirectUrl = $session->data('url');

$whm->resellers()->promote('reseller1');
$whm->resellers()->setLimits('reseller1', ['enable_account_limit' => 1, 'account_limit' => 30]);
$whm->resellers()->setPackageLimit('reseller1', 'starter', allowed: true, number: 20);

$whm->packages()->create('starter', ['quota' => 5120, 'bwlimit' => 51200, 'maxaddons' => 1]);

$whm->dnsZones()->create('customer1.com', '203.0.113.10');
$whm->dnsZones()->addRecord('customer1.com', [
    'name' => 'mail',
    'type' => 'A',
    'address' => '203.0.113.10',
    'ttl' => 3600,
]);

$whm->ips()->add('203.0.113.25', '255.255.255.0');
$whm->ips()->assignToSite('customer1.com', '203.0.113.25');

$whm->security()->unblockBrute('198.51.100.7');
$whm->security()->whitelist('203.0.113.50', 'office VPN');

$whm->backups()->queueRestore('customer1', '2026-07-20');
$whm->backups()->activateRestoreQueue();

$whm->php()->setVhostVersion('ea-php83', 'example.com', 'shop.example.com');

$whm->autoSsl()->setProvider('LetsEncrypt');
$whm->autoSsl()->checkUser('customer1');

$whm->config()->setTweakSetting('maxemailsperhour', 200);

$whm->server()->serviceStatus('httpd');
$whm->server()->restartService('exim');

$response = $cpanel->mysql()->databases();

$response->success;              // bool
$response->data;                 // mixed — the payload's data section
$response->data('acct');         // keyed access with optional default
$response->errors;               // list<string>
$response->messages;             // list<string>
$response->warnings;             // list<string>
$response->raw;                  // the complete decoded JSON payload

use ChuckBartowski\CpanelSdk\Exception\ApiException;
use ChuckBartowski\CpanelSdk\Exception\CpanelSdkExceptionInterface;

try {
    $cpanel->email()->create('support', 'example.com', $password);
} catch (ApiException $e) {
    $this->logger->warning('cPanel rejected the mailbox', ['errors' => $e->getErrors()]);
} catch (CpanelSdkExceptionInterface $e) {
    throw new ProvisioningUnavailableException(previous: $e);
}

$response = $cpanel->client()->uapi('Email', 'add_pop', $params, 'POST');
if (!$response->success) {
    // $response->errors, $response->raw
}

use ChuckBartowski\CpanelSdk\Client\CpanelClient;
use ChuckBartowski\CpanelSdk\Cpanel;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;

$http = new MockHttpClient(new JsonMockResponse(['status' => 1, 'data' => []]));
$cpanel = new Cpanel(new CpanelClient('host', 'user', 'token', 2083, true, 30.0, $http));