Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Last active September 23, 2019 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finalwebsites/162e168b3539b12005141113277b5edd to your computer and use it in GitHub Desktop.
Save finalwebsites/162e168b3539b12005141113277b5edd to your computer and use it in GitHub Desktop.
Start your VPS on a different host node using PHP and the UpCloud API.
<?php
// Enter here your server UID and the login / password
$uid = 'xxx-xxx-xxx-xxx-xxx';
$host = 'https://api.upcloud.com/1.2/server/'.$uid;
$username = 'XXX';
$password = 'XXXXXXXX';
<?php
require_once 'conf.php';
// Do a server detail request and obtain the current host id
$ch = curl_init($host);
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode($username.':'.$password)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);
$array = json_decode($return, true);
// Add the current host id to avoid this one during the start request
echo $array['server']['host'];
<?php
require_once 'conf.php';
$start_data['server']['avoid_host'] = 'PLACE HERE THE HOST ID';
$start_data_string = json_encode($start_data);
// start the server
$ch = curl_init($host.'/start');
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode($username.':'.$password),
'Content-Length: ' . strlen($start_data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $start_data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($start_data_string)
)
);
$response = curl_exec($ch);
curl_close($ch);
<?php
require_once 'conf.php';
// Shutdown the server
$stop_data['stop_server'] = array('stop_type' => 'soft', 'timeout' => '60');
$stop_data_string = json_encode($stop_data);
$ch = curl_init($host.'/stop');
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode($username.':'.$password),
'Content-Length: ' . strlen($stop_data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $stop_data_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$resp = curl_exec($ch);
curl_close($ch);
@finalwebsites
Copy link
Author

finalwebsites commented Aug 20, 2019

Before you can use this script you need to enable API access via the UpCloud controle panel.

  1. Change the settings for the conf.php file
  2. Run the file server.php to get the host id
  3. Run stop.php to stop your server (you can do that via the UpCloud panel too)
  4. Edit start.php and add the host id you got before
  5. Run the start.php script

I suggest to run these PHP scripts via your laptop's terminal application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment