In the TransIP control panel, you have the option to give your VPS a custom name (via 'manage' -> 'change name'). The name you give it in this way is a 'description' in our systems.
If you use our REST API, you might want an easy way to search for the description of a VPS.
In this guide, we will show an example of how to create a function in PHP that allows you to search for the description of your VPS.
For the steps in this guide, we use the PHP library of our REST API and assume that you have read the following articles:
- Enabling the API in your TransIP account & generating a private key for authentication
- Setting up automatic API authentication
Step 1
Create a file where you add the code, for example as below, or go to step 2 if you want to add the code to an existing .PHP file
nano ~/vpssearch.php
Add the following code to the file:
<?php
require_once(__DIR__ . '/Authenticate.php');
function getVpsByDescription($api, $vpsDescription) {
$vpsList = $api->vps()->getAll();
foreach ($vpsList as $vps) {
if ($vps->getDescription() === $vpsDescription) {
return $vps;
}
}
return null;
}
$vps = getVpsByDescription($api, 'my-vps-description');
if ($vps) {
echo ($vps->name);
}
The 'getVpsByDescription' function takes two parameters:
- $api connects to the TransIP API.
- $vpsDescription is the custom name/description of your VPS that the function searches for.
The 'getVpsByDescription' function retrieves a list of all VPSs in your TransIP account and searches them all for the $vpsDescription, in this example 'my-vps-description'. If a match is found, you get the object (not an array) $vps containing all the data of the corresponding VPS.
In this example, the name of the VPS is displayed, but you are of course free to modify what is done when the VPS is found.
Step 3
You can now test the functionality directly with:
php ~/vpssearch.php
This brings us to the end of this article. If you have any questions based on this guide, do not hesitate to contact our support department. You can reach them via the 'Contact us' button at the bottom of this page.