PHP code example of praxisnetau / serverpilot-api

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

    

praxisnetau / serverpilot-api example snippets


use ServerPilot\ServerPilotAPI;

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

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->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');

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!
    
}

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 = $api->getLastActionID();  // ID of the last action

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!
        }
        
    }
    
}

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

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