Skip to content

Examples

API ID/KEY are used for the authentication on SolusVM. It is required to browse to Dashboard > Configuration > API Access and add API user. It is binded to the IP address of the server where the script is executed.

Create a virtual server

<?php

// Url to the admin API
$url = "https://IP_ADDRESS:5656/api/admin";
$postfields["id"] = "hashed_id";
$postfields["key"] = "hashed_key";
$postfields["action"] = "vserver-create";
$postfields["type"]="kvm";
$postfields["node"] = "kvmc7node";
$postfields["hostname"] = "my.virtualserver.tld";
$postfields["plan"] = "service1";
$postfields["template"] = "linux-centos-7-x86_64-minimal-latest";
$postfields["nodegroup"] = "0";
$postfields["password"] = "qwerty123";
$postfields["username"] = "johndoe";
$postfields["ips"] = "1";
$postfields["randomipv4"] = "true";

// Send the query to the solusvm master
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "/command.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
curl_close($ch);

// Parse the returned data and build an array
preg_match_all('/\<(.\*?)>([\^\<]+)\<\\/\\\\1>/i', $data, $match);
$result = array();
foreach ($match[1] as $x => $y)
{
    $result[$y] = $match[2][$x];
}

print_r($match);
echo "================";
echo "";

Template name should be a file name of the template without extension, e.g. linux-centos-7-x86_64-minimal-latest instead of linux-centos-7-x86_64-minimal-latest.gz

If the execution of the script takes too much time and fails with time out error, consider tuning CURLOPT_TIMEOUT option

List all virtual servers

<?php
// Url to the admin API
$url = "https://IP_ADDRESS:5656/api/admin";
$postfields["id"] = "hashed_id";
$postfields["key"] = "hashed_key";
$postfields["action"] = "node-virtualservers";
$postfields["nodeid"]="2";

// Send the query to the solusvm master
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "/command.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
curl_close($ch);

// Parse the returned data and build an array
preg_match_all('/\<(.\*?)>([\^\<]+)\<\\/\\\\1>/i', $data, $match);
$result = array();
foreach ($match[1] as $x => $y)
{
    $result[$y] = $match[2][$x];
}

print_r($match);
echo "================";
echo "";

Fetching the information about the virtual server

<?php
// Url to the admin API
$url = "https://IP_ADDRESS:5656/api/admin";
$postfields["id"] = "hashed_id";
$postfields["key"] = "hashed_key";
$postfields["vserverid"]="246";

// Send the query to the solusvm master
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "/command.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
curl_close($ch);

// Parse the returned data and build an array
preg_match_all('/\<(.\*?)>([\^\<]+)\<\\/\\\\1>/i', $data, $match);
$result = array();
foreach ($match[1] as $x => $y)
{
    $result[$y] = $match[2][$x];
}
print_r($match);
echo "================";

Reboot the virtual server

<?php
// Url to the admin API
$url = "https://\<MASTER IP>:5656/api/admin";
$postfields["id"] = "EXAMPLEIDKEY";
$postfields["key"] = "EXAMPLEIDPASSWORD";
$postfields["action"] = "reboot";
$postfields["vserverid"] = "100";

// Send the query to the solusvm master
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "/command.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
curl_close($ch);

// Parse the returned data and build an array
preg_match_all('/\<(.\*?)>([\^\<]+)\<\\/\\\\1>/i', $data, $match);
$result = array();
foreach ($match[1] as $x => $y)
{
    $result[$y] = $match[2][$x];
}