PHP code example of itxshakil / cpanel-whm

1. Go to this page and download the library: Download itxshakil/cpanel-whm 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/ */

    

itxshakil / cpanel-whm example snippets


use Itxshakil\CpanelWhm\Data\NewAccount;
use Itxshakil\CpanelWhm\Facades\Whm;

$account = Whm::accounts()->create(new NewAccount(
    username: 'acme',
    domain: 'acme.example',
    package: 'starter',
));

Whm::suspensions()->suspend('acme', 'Unpaid invoice #1042');

return redirect()->away(Whm::sessions()->create('acme')->url);   // one-click cPanel login

use Itxshakil\CpanelWhm\Enums\SearchType;
use Itxshakil\CpanelWhm\Support\Filter;

$created = Whm::accounts()->create(new NewAccount('acme', 'acme.example', package: 'starter'));
$created->username;      // what WHM assigned
$created->nameservers;   // ['ns1.example.com', 'ns2.example.com']
$created->password;      // the one you gave, or a strong generated one

Whm::accounts()->list();                                         // Collection<Account>
Whm::accounts()->list(Filter::where('domain', 'contains', 'acme'));
Whm::accounts()->search('starter', SearchType::Package, exact: true);
Whm::accounts()->find('acme');                                   // ?Account
Whm::accounts()->isUsernameAvailable('acme');                    // local rules, then WHM
Whm::accounts()->changePackage('acme', 'pro');
Whm::accounts()->changePassword('acme', $password);
Whm::accounts()->modify('acme', ['CONTACTEMAIL' => '[email protected]']);
Whm::accounts()->remove('acme');

Whm::suspensions()->suspend('acme', 'Unpaid', lock: true);   // lock: the reseller cannot unsuspend
Whm::suspensions()->unsuspend('acme');
Whm::suspensions()->list();                                  // Collection<SuspendedAccount>

Whm::packages()->list();                                     // Collection<Package>
Whm::packages()->find('starter')?->diskQuota;

Whm::quotas()->setDiskQuota('acme', 10_240);                 // MB, null = unlimited
Whm::quotas()->setBandwidthLimit('acme', null);

use Itxshakil\CpanelWhm\Enums\SessionService;

$session = Whm::sessions()->create('acme', SessionService::Cpanel, app: 'Email_Accounts');
$session->url;         // one use; the session ends after 15 minutes idle
$session->expiresAt;

$mailboxes = Whm::asUser('acme')->uapi('Email', 'list_pops');
$mailboxes->toArray();
$mailboxes->warnings;

Whm::asUser('acme')->uapi('Email', 'add_pop', ['email' => 'info', 'password' => $password]);

$mailbox = Whm::asUser('acme')->email()->create('[email protected]', quotaMegabytes: 1024);
$mailbox->password;    // generated when you pass none

Whm::asUser('acme')->mysql()->createDatabase('acme_shop');
Whm::asUser('acme')->mysql()->grant('acme_app', 'acme_shop');

Whm::asUser('acme')->email()->list(page: 2, perPage: 50);   // UAPI pagination; also mysql()->databases() and users()

use Itxshakil\CpanelWhm\Data\DnsRecord;

Whm::dns()->add('acme.example', DnsRecord::a('shop', '203.0.113.20'), DnsRecord::txt('@', 'v=spf1 -all'));
$www = Whm::dns()->zone('acme.example')->first('www', 'A');   // null when there's no such record

if ($www !== null) {
    Whm::dns()->update('acme.example', $www->withData('203.0.113.30'));
}

Whm::domains()->createSubdomain('blog.acme.example', 'public_html/blog');
Whm::domains()->owner('acme.example');                         // "acme"

Whm::usage()->account('acme')->disk?->percentUsed();           // 72.4
Whm::backups()->restore('acme', Whm::backups()->dates()->last());

Whm::resellers()->create('res1');
Whm::resellers()->setPrivileges('res1', ['create-acct', 'suspend-acct', 'list-accts']);

Whm::ssl()->autoSslProblems('acme');
Whm::tokens()->expiringWithin(14);

Whm::api()->ipAddressManagement()->listips();
Whm::api()->sslCertificates()->fetchSslVhosts();
Whm::asUser('acme')->api()->email()->addPop(email: 'info', password: $password, quota: 1024);
Whm::asUser('acme')->api()->mysql()->createDatabase(name: 'acme_shop');

use Itxshakil\CpanelWhm\Enums\HttpMethod;

$response = Whm::call('listips');
$response->get('ip.0.ip');           // dot notation into data
$response->warnings();               // warnings WHM attached to a "successful" call

Whm::call('modifyacct', $params, HttpMethod::Post);

Whm::withHttpMethod(HttpMethod::Post)->accounts()->list();   // e.g. a WAF that only allows POST to /json-api

$results = Whm::batch()->add('accountsummary', ['user' => 'acme'])->add('version')->send();
$results[0]->get('acct.0.domain');

// config/cpanel-whm.php → 'connections' => ['main' => [...], 'ca-1' => [...]]
Whm::connection('ca-1')->accounts()->list();

use Itxshakil\CpanelWhm\Facades\Whm;

public function test_an_overdue_invoice_suspends_the_account(): void
{
    $whm = Whm::fake([
        'suspendacct' => Whm::response(),
        'accountsummary' => Whm::failure('Account does not exist.'),
    ]);

    // ... run your code ...

    $whm->assertCalled('suspendacct', fn (array $params) => $params['user'] === 'acme');
    $whm->assertNotCalled('removeacct');
}

Whm::withoutRetries()->server()->version();   // one attempt, e.g. in a health check
bash
php artisan whm:doctor