rene-roscher/proxmoxve

一个简单的PHP 5.5+ Proxmox API客户端。

v4.0.7 2017-03-16 23:12 UTC

This package is auto-updated.

Last update: 2024-09-17 13:01:44 UTC


README

PHP 5.4+库允许您通过API与Proxmox服务器交互。

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

如果您发现任何错误、错别字或者检测到某些事情没有按预期工作,请提交问题或通过@lumaserv发推文给我。我会尽快发布修复补丁。

需要PHP 5.3库版本吗?发布版本中搜索适合您需求的版本,我推荐使用2.1.1版本。

安装

推荐使用Composer进行安装,如果您还没有Composer,您还在等什么?

在您的项目根目录中执行以下命令

$ composer require lumaserv/proxmoxve ~4.0.4

或者将以下内容添加到您的composer.json文件中

{
    "require": {
        "lumaserv/proxmoxve": "~4.0.4"
    }
}

然后执行安装

$ composer install --no-dev

用法

<?php

// Require the autoloader
require_once 'vendor/autoload.php';

// Use the library namespace
use ProxmoxVE\Proxmox;

// Create your credentials array
$credentials = [
    'hostname' => 'proxmox.server.com',  // Also can be an IP
    'username' => 'root',
    'password' => 'secret',
];

// realm and port defaults to 'pam' and '8006' but you can specify them like so
$credentials = [
    'hostname' => 'proxmox.server.com',
    'username' => 'root',
    'password' => 'secret',
    'realm' => 'pve',
    'port' => '9009',
];

// Then simply pass your credentials when creating the API client object.
$proxmox = new Proxmox($credentials);

$allNodes = $proxmox->get('/nodes');

print_r($allNodes);

示例输出

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [disk] => 2539465464
                    [cpu] => 0.031314446882002
                    [maxdisk] => 30805066770
                    [maxmem] => 175168446464
                    [node] => mynode1
                    [maxcpu] => 24
                    [level] => 
                    [uptime] => 139376
                    [id] => node/mynode1
                    [type] => node
                    [mem] => 20601992182
                )

        )

)

使用自定义凭据对象

也可以通过传递包含连接Proxmox服务器所需所有相关数据的自定义对象来创建ProxmoxVE实例

<?php
// Once again require the autoloader
require_once 'vendor/autoload.php';

// Sample custom credentials class
class CustomCredentials
{
    public function __construct($host, $user, $pass)
    {
        $this->hostname = $host;
        $this->username = $user;
        $this->password = $pass;
    }
}

// Create ProxmoxVE instance by passing your custom credentials object
$credentials = new CustomCredentials('proxmox.server.com', 'root', 'secret');
$proxmox = new ProxmoxVE\Proxmox($credentials);

// Then you can use it, for example create a new user.

// Define params
$params = [
    'userid' => 'new_user@pve',  // Proxmox requires to specify the realm (see the docs)
    'comment' => 'Creating a new user',
    'password' => 'canyoukeepasecret?',
];

// Send request passing params
$result = $proxmox->create('/access/users', $params);

// If an error occurred the 'errors' key will exist in the response array
if (isset($result['errors'])) {
    error_log('Unable to create new proxmox user.');
    foreach ($result['errors'] as $title => $description) {
        error_log($title . ': ' . $description);
    }
} else {
    echo 'Successful user creation!';
}

使用自定义凭据对象很有用,当您的应用程序使用一些包含连接数据的ORM模型时,例如,您可以传递一个包含凭据的Eloquent模型。

许可证

本项目采用MIT许可证发布。有关详细信息,请参阅附带的LICENSE文件。