PHP code example of chuckbartowski / proxmox-sdk

1. Go to this page and download the library: Download chuckbartowski/proxmox-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 / proxmox-sdk example snippets


$vmid = (int) $proxmox->cluster()->nextId()->data;
$task = $proxmox->qemu()->clone('pve1', 9000, $vmid, ['name' => 'web01', 'full' => 1]);
$proxmox->nodes()->waitForTask('pve1', $task->upid());
$proxmox->qemu()->start('pve1', $vmid);

use ChuckBartowski\ProxmoxSdk\Client\ProxmoxClient;
use ChuckBartowski\ProxmoxSdk\Proxmox;

$proxmox = new Proxmox(new ProxmoxClient(
    host: 'pve.example.com',
    tokenId: 'automation@pve!sdk',
    tokenSecret: getenv('PVE_TOKEN_SECRET'),
));

$vmid = (int) $proxmox->cluster()->nextId()->data;

$task = $proxmox->qemu()->clone('pve1', 9000, $vmid, ['name' => 'web01', 'full' => 1]);
$proxmox->nodes()->waitForTask('pve1', $task->upid());

$proxmox->qemu()->start('pve1', $vmid);

new ProxmoxClient(
    string $host,
    string $tokenId = '',                     // 'user@realm!tokenname' (preferred)
    string $tokenSecret = '',
    string $username = '',                    // fallback: ticket auth
    string $password = '',
    string $realm = 'pam',                    // appended when $username has no '@'
    int $port = 8006,
    bool $verifySsl = true,
    float $timeout = 30.0,
    ?HttpClientInterface $httpClient = null,  // inject your own (retries, proxy, mock…)
);

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

$proxmox->access()->createToken('automation@pve', 'sdk', ['privsep' => 1]);

$task = $proxmox->qemu()->clone('pve1', 9000, 101, ['full' => 1]);

$task->upid();                                        // 'UPID:pve1:...:qmclone:9000:root@pam:'

$proxmox->nodes()->waitForTask('pve1', $task->upid(), timeout: 600.0, pollInterval: 2.0);

$proxmox->qemu()->create('pve1', 101, [
    'name' => 'web01',
    'memory' => 4096,
    'cores' => 2,
    'net0' => 'virtio,bridge=vmbr0',
    'scsi0' => 'local-lvm:32',
    'ide2' => 'local:iso/debian-12.iso,media=cdrom',
]);

$proxmox->lxc()->create('pve1', 200, [
    'ostemplate' => 'local:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst',
    'hostname' => 'app01',
    'memory' => 1024,
    'rootfs' => 'local-lvm:8',
    'net0' => 'name=eth0,bridge=vmbr0,ip=dhcp',
    'unprivileged' => 1,
]);

$proxmox->cluster()->createPool('customers');
$proxmox->cluster()->updatePool('customers', ['vms' => '101,102']);

$proxmox->backups()->run('pve1', ['vmid' => '101', 'storage' => 'backup-nfs', 'mode' => 'snapshot', 'compress' => 'zstd']);

$proxmox->access()->createUser('deploy@pve', ['password' => 'S3cret!', 'groups' => 'automation']);
$proxmox->access()->updateAcl('/vms/101', 'PVEVMAdmin', ['users' => 'deploy@pve']);

$response = $proxmox->cluster()->resources('vm');

$response->success;          // bool
$response->statusCode;       // int
$response->data;             // mixed — the payload's data section
$response->data('version');  // keyed access with optional default
$response->errors;           // list<string> — field errors flattened as 'field: message'
$response->raw;              // complete decoded payload (envelope 

use ChuckBartowski\ProxmoxSdk\Exception\ApiException;
use ChuckBartowski\ProxmoxSdk\Exception\ProxmoxSdkExceptionInterface;

try {
    $task = $proxmox->qemu()->clone('pve1', 9000, $vmid, ['full' => 1]);
    $proxmox->nodes()->waitForTask('pve1', $task->upid());
} catch (ApiException $e) {
    $this->logger->error('VM provisioning failed', ['errors' => $e->getErrors()]);
} catch (ProxmoxSdkExceptionInterface $e) {
    throw new ProvisioningUnavailableException(previous: $e);
}

$response = $proxmox->client()->post('/nodes/pve1/qemu', $params);
if (!$response->success) {
    // $response->statusCode, $response->errors, $response->raw
}

use ChuckBartowski\ProxmoxSdk\Client\ProxmoxClient;
use ChuckBartowski\ProxmoxSdk\Proxmox;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;

$http = new MockHttpClient(new JsonMockResponse(['data' => []]));
$proxmox = new Proxmox(new ProxmoxClient('host', tokenId: 't@pve!x', tokenSecret: 's', httpClient: $http));