PHP code example of enterpriseve / eve2pve-api-php

1. Go to this page and download the library: Download enterpriseve/eve2pve-api-php 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/ */

    

enterpriseve / eve2pve-api-php example snippets



Corsinvest\ProxmoxVE\Api\PveClient;

// Create client instance
$client = new PveClient("your-proxmox-host.com");

// Authenticate
if ($client->login('root@pam', 'password')) {
    // Get cluster status
    $status = $client->getCluster()->status();
    echo "Cluster: {$status->getResponse()->data[0]->name}\n";

    // List all VMs
    $vms = $client->getNodes()->get("pve1")->getQemu()->vmlist();
    foreach ($vms->getResponse()->data as $vm) {
        echo "VM {$vm->vmid}: {$vm->name} - {$vm->status}\n";
    }
}


// Create and configure a new VM with individual parameters (PHP 8.1+)

// Using named arguments (PHP 8.0+) for clarity
$result = $client->getNodes()->get("pve1")->getQemu()->createVm(
    vmid: 100,
    name: 'production-web-server',
    memory: 4096,
    cores: 4,
    net0: 'virtio,bridge=vmbr0',
    scsi0: 'local-lvm:32',
    acpi: true,
    balloon: 0,
    cpu: 'host',
    numa: false,
    hotplug: 'network,disk,cpu,memory',
    tags: 'production,web',
    template: false
);

if ($result->isSuccessStatusCode()) {
    echo "VM created successfully!\n";

    // Start the VM
    $startResult = $client->getNodes()->get("pve1")->getQemu()->get(100)->getStatus()->getStart()->vmStart();
    if ($startResult->isSuccessStatusCode()) {
        echo "VM started successfully!\n";
    }
} else {
    echo "Error creating VM: " . $result->getError() . "\n";
}


// Monitor cluster resources
$resources = $client->getCluster()->resources();

foreach ($resources->getResponse()->data as $resource) {
    if ($resource->type === 'vm' && $resource->status === 'running') {
        $cpuPercent = round($resource->cpu * 100, 2);
        $memPercent = round(($resource->mem / $resource->maxmem) * 100, 2);

        echo "VM {$resource->vmid}: CPU {$cpuPercent}%, RAM {$memPercent}%\n";
    }
}


// Create backup for multiple VMs
$vmids = [100, 101, 102];

$backup = $client->getNodes()->get("pve1")->getVzdump()->create(
    vmid: implode(',', $vmids),  // Comma-separated VM IDs
    storage: 'backup-storage',   // Storage target
    mode: 'snapshot',            // Backup mode
    compress: 'zstd'             // Compression algorithm
);

$taskId = $backup->getResponse()->data;
echo "Backup started with task: {$taskId}\n";
text
   ______                _                      __
  / ____/___  __________(_)___ _   _____  _____/ /_
 / /   / __ \/ ___/ ___/ / __ \ | / / _ \/ ___/ __/
/ /___/ /_/ / /  (__  ) / / / / |/ /  __(__  ) /_
\____/\____/_/  /____/_/_/ /_/|___/\___/____/\__/

Proxmox VE API Client for PHP (Made in Italy)
bash
composer