upcloudltd/upcloud-php-api

v3.0.0 2023-12-01 12:03 UTC

README

UpCloud php api test Latest Stable Version License Total Downloads

此PHP API客户端库提供了与UpCloud API的集成,允许用于管理UpCloud资源的操作。客户端是一个使用HTTPS连接到API的Web服务接口。API尽可能遵循RESTful Web服务的原则。

所有API操作的基准URL为https://api.upcloud.com/,并需要使用UpCloud用户名和密码进行基本身份验证。出于安全原因,我们建议创建一个子账户,专门用于API通信。这允许您通过服务器、存储和标签限制API访问,确保您永远不会意外影响关键系统。

注意:在实际生产使用之前,请彻底测试所有用例。建议为客户端测试/开发使用单独的UpCloud账户。

目录

要求

使用此库需要PHP版本8.0或更高版本。

安装

Composer

要使用Composer安装绑定,请将以下内容添加到composer.json

{
  "require": {
    "upcloudltd/upcloud-php-api": "v2.0.0"
  }
}

然后运行composer install

upcloudltd/upcloud-php-api可以从packagist.org找到,https://packagist.org.cn/packages/upcloudltd/upcloud-php-api

手动安装

下载文件并包含autoload.php

    require_once('/path/to//vendor/autoload.php');

测试

要运行单元测试

composer install

./vendor/bin/phpunit

使用

请遵循安装程序,然后运行以下操作(注意:在实际生产应用程序中,您应该使用例如ENV变量而不是将凭据直接插入代码和版本控制)

<?php
require_once(__DIR__ . '/vendor/autoload.php');

$api_instance = new Upcloud\ApiClient\Upcloud\AccountApi();
$config = $api_instance->getConfig();
$config->setUsername('YOUR UPCLOUD USERNAME');
$config->setPassword('YOUR UPCLOUD PASSWORD');

try {
    $result = $api_instance->getAccount();
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling AccountApi->getAccount: ', $e->getMessage(), PHP_EOL;
}

?>

创建服务器

<?php
require_once(__DIR__ . '/vendor/autoload.php');

$api_instance = new Upcloud\ApiClient\Upcloud\AccountApi();
$config = $api_instance->getConfig();
$config->setUsername('YOUR UPCLOUD USERNAME');
$config->setPassword('YOUR UPCLOUD PASSWORD');

try {
        $result = $api_instance->getAccount();
            print_r($result);
} catch (Exception $e) {
        echo 'Exception when calling AccountApi->getAccount: ', $e->getMessage(), PHP_EOL;
}

$server = new Upcloud\ApiClient\Model\Server();
$server->setTitle('php-test-machine');
$server->setZone('fi-hel1');
$server->setHostname('phptest');
$server->setPlan('1xCPU-1GB');

$storage = new Upcloud\ApiClient\Model\StorageDevice();
$storage->setStorage('01000000-0000-4000-8000-000030060200');
$storage->setSize(50.0);
$storage->setAction('clone');
$storage->setTitle('php-test-storage');

$storage_devices = new Upcloud\ApiClient\Model\ServerStorageDevices();
$storage_devices->setStorageDevice([$storage]);

$server->setStorageDevices($storage_devices);

$server_request = new Upcloud\ApiClient\Model\CreateServerRequest();
$server_request->setServer($server);
$api_instance = new Upcloud\ApiClient\Upcloud\ServerApi();
try {
        $result = $api_instance->createServer($server_request);
            print_r($result);
} catch (Exception $e) {
        echo 'Exception when calling ServerApi->createServer: ', $e->getMessage(), PHP_EOL;
}

?>

文档

所有URI都是相对于https://api.upcloud.com/1.3

模型文档

授权文档

在开发API应用程序时,建议将用户名和密码存储在单独的配置文件中,以避免意外发布您的账户凭证。

baseAuth

  • 类型:HTTP基本认证
  • 用户名:您的UpCloud API用户名
  • 密码:您的UpCloud API用户密码

问题

发现了错误,在使用客户端时遇到问题,或关于库的其他任何问题?请在此处创建新问题以取得联系。

许可证

本项目遵循MIT许可证进行分发,有关更多信息,请参阅LICENSE.txt。