pcextreme / cloudstack

Cloudstack PHP 客户端

0.3.0 2018-10-08 13:00 UTC

This package is auto-updated.

Last update: 2024-09-25 07:46:55 UTC


README

github-readme_header

PHP Cloudstack 客户端

Latest Version Software License Build Status Scrutinizer Coverage Status

此软件包可以轻松地将 Cloudstack API 集成到您的 PHP 应用程序中。

要求

以下版本的 PHP 受支持:

  • PHP 7.1 或更高版本

已停止支持 PHP 5.x

为了更严格的类型提示和返回类型,我们在新版本中停止了对旧 PHP 版本的支持。

请注意:PHP 5.x 的一般支持即将结束,请查看 PHP 文档以获取有关 PHP 版本支持的更多信息。

如果您仍然需要支持 PHP 5.x 或 HHVM,可以使用此软件包的 ~0.2 版本

安装

使用 composer require 将客户端添加到您的 composer.json 文件中,请检查 Composer 文档以获取有关 Composer 的更多信息。

$ composer require pcextreme/cloudstack

用法

有多种方式可以使用此软件包与 Cloudstack API 交互。最简单且推荐的方式是直接在客户端上调用 Cloudstack '命令'。

客户端内部使用 __call 魔法方法解析命令。命令将被自动解析,提供的选项将得到验证。如果一切顺利,API 响应也将自动解析。

客户端使用缓存文件夹中存储的 API 列表映射来确定 API 命令是否存在以及是否提供了所有必需的参数。此列表使用 Cloudstack 的 listApis 命令生成。

<?php

include(__DIR__.'/vendor/autoload.php');

use PCextreme\Cloudstack\Client;

$client = new Client([
    'urlApi'    => 'https://api.auroracompute.eu/ams',
    'apiKey'    => 'YOUR-API-KEY',
    'secretKey' => 'YOUR-SECRET-KEY',
]);

var_dump($client->listAccounts(['name' => 'admin', 'listall' => 'true']));

直接调用命令方法

也可以绕过 __call 魔法方法,直接调用 command 方法。

<?php

// Using an existing client instance.

$client->command('listAccounts', ['name' => 'admin', 'listall' => 'true']);

手动访问 API

如果由于某种原因删除了 cache/api_list.php、过时或损坏,您可以通过手动构建请求直接访问 API。这绕过了所有之前提到的检查,但仍会解析响应。

<?php

// Using an existing client instance.

$command = 'listAccounts';
$options = ['name' => 'admin', 'listall' => 'true'];

$method  = $client->getCommandMethod($command);
$url     = $client->getCommandUrl($command, $options);
$request = $client->getRequest($method, $url, $options);

$accounts = $client->getResponse($request);

更新 API 列表

您可以使用 bin/cloudstack CLI 更新或重新生成提供的 API 列表。

$ php bin/cloudstack api:list

您还可以获取可用 CLI 命令的列表。

$ php bin/cloudstack

贡献

此软件包是开源的,欢迎任何人提交问题和拉取请求。在提交拉取请求之前,请阅读我们的简短贡献指南

鸣谢

许可协议

MIT 许可协议 (MIT)。有关更多信息,请参阅许可文件