praxisnetau/serverpilot-api

使用PHP实现的对ServerPilot API的包装和数据模型。

1.0.2 2016-11-28 23:28 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:58:43 UTC


README

使用PHP实现的ServerPilot API的包装和数据模型。

要求

  • PHP 5.3.3或更高版本
  • curl/curl 1.4或更高版本
  • PHP JSON扩展
  • ServerPilot账号(+有效的客户端ID和API密钥)

安装

您可以通过命令行使用Composer在项目中安装API

composer require praxisnetau/serverpilot-api ~1.0

...或者将其添加到项目的composer.json文件中

require: {
    "praxisnetau/serverpilot-api": "~1.0"
}

...然后执行以下命令

composer install

使用方法

您需要通过ServerPilot控制面板创建一个客户端IDAPI密钥。一旦您有了这些,您可以通过以下方式创建API实例

use ServerPilot\ServerPilotAPI;

$api = ServerPilotAPI::inst('<client-id>', '<api-key>');

如果凭证无效或API遇到任何其他问题,API将抛出Exception,因此将API代码包裹在try/catch块中是个好主意,例如

try {
    
    $api = ServerPilotAPI::inst('<client-id>', '<api-key>');
    
    $servers = $api->servers->listAll();
    
} catch (\Exception $e) {
    
    // Whoops, something went wrong!
    
}

如果您喜欢,也可以通过配置对象设置凭证

$api = ServerPilotAPI::inst();

$api->config()->set('client-id', '<client-id>');
$api->config()->set('api-key', '<api-key>');

命令

API通过将属性调用路由到一系列命令对象来工作。默认情况下,这些属性名称和方法与ServerPilot API文档中给出的名称和方法匹配,即

$api->servers;   // For server commands
$api->sysusers;  // For system user commands
$api->apps;      // For app commands
$api->dbs;       // For database commands
$api->actions;   // For action status commands

服务器

处理与服务器相关的操作。

$api->servers->listAll();
$api->servers->get('serverid');
$api->servers->create('name');
$api->servers->update('serverid', (boolean) $firewall, (boolean) $autoupdates);
$api->servers->delete('serverid');

系统用户

处理系统用户操作。

$api->sysusers->listAll();
$api->sysusers->get('sysuserid');
$api->sysusers->create('serverid', 'username', 'password');
$api->sysusers->update('sysuserid', 'newpassword');
$api->sysusers->delete('sysuserid');

应用

处理应用操作。

$api->apps->listAll();
$api->apps->get('appid');
$api->apps->create('appname', 'sysuserid', 'runtime', ['domains'], ['wordpress']);
$api->apps->update('appid', 'runtime', ['domains']);
$api->apps->delete('appid');

数据库

处理与数据库相关的操作。

$api->dbs->listAll();
$api->dbs->get('databaseid');
$api->dbs->create('appid', 'dbname', ['name' => 'username', 'password' => 'password']);
$api->dbs->update('databaseid', ['id' => 'userid', 'password' => 'newpassword']);
$api->dbs->delete('databaseid');

动作

检索动作的状态。

$api->actions->get('actionid');

数据模型

尽管API命令功能强大,但有时直接通过API发出一系列命令可能会有些笨拙。这就是数据模型的作用。为API命令的每个区域提供了模型对象,包括

  • 动作
  • 动作状态
  • 应用
  • 数据库
  • 数据库用户
  • 服务器
  • 系统用户

而不是返回解码后的JSON数据,API返回数据模型类单个实例,或返回数据模型类数组(当返回多个结果时)。

这些模型对象是API感知的,可以直接操作而无需单独调用API。所有模型对象都有update()delete()方法。例如

if ($server = $api->servers->get('<server-id>')) {
    
    $server->setFirewall(true);
    $server->setAutoUpdates(true);
    
    $server->update();  // <-- server is updated via the API
    
}

或者,考虑以下

if ($app = $api->apps->get('<app-id>')) {
    
    $app->setRuntime('php7.1');
    
    $app->addDomain('myapp.mydomain.com');
    $app->removeDomain('myapp.anotherdomain.com');
    
    $app->update();  // <-- app is updated via the API
    
}

厌倦了那个旧应用?为什么不删除它!

if ($app = $api->apps->get('<app-id>')) {
    
    $app->delete();  // <-- app has been deleted via the API, be careful!
    
}

检索动作状态

每次API执行更改操作时,都会记录一个动作。查看上次执行的动作可能很有用,您可以通过在API上调用getLastAction()来检索此对象

if ($action = $api->getLastAction()) {
    
    $id     = $action->getID();      // ID of the action
    $data   = $action->getData();    // data returned by the API for the action
    $object = $action->getObject();  // data model object created for the action
    
}

如果您只需要最后动作的ID,您可以使用getLastActionID()

$id = $api->getLastActionID();  // ID of the last action

您还可以进一步使用getStatus方法检查动作的状态,该方法返回一个ActionStatus对象

if ($action = $api->getLastAction()) {
    
    if ($status = $action->getStatus()) {
        
        $id          = $status->getID();           // ID of the action status
        $status      = $status->getStatus();       // status text ('open', 'error', or 'success')
        $serverId    = $status->getServerID();     // ID of the server
        $dateCreated = $status->getDateCreated();  // timestamp of the status
        
        if ($status->isOpen()) {
            // The action hasn't finished yet!
        }
        
        if ($status->isError()) {
            // Whoops, something went wrong!
        }
        
        if ($status->isSuccessful()) {
            // Huzzah, it worked!
        }
        
    }
    
}

或者,您可以直接从API使用getLastActionStatus()检索最后动作的状态

$status = $api->getLastActionStatus();  // returns an ActionStatus object for last action

DateTime访问器

API返回的所有属性都有DateTime访问器方法。例如

if ($server = $api->servers->get('<server-id>')) {
    
    $lastconn = $server->getLastConnected();        // returns integer timestamp
    $datetime = $server->getLastConnectedObject();  // returns DateTime object
    
}

待办事项

  • 为应用实现付费计划的SSL功能
  • 扩展数据模型以支持额外功能