cancel
Showing results for 
Search instead for 
Did you mean: 

Tech Tip: Using the RESTful Control API with PHP - startstopvs

The following code uses Stingray's RESTful API to enable or disabled a specific Virtual Server.   The code is written in PHP and uses STMRESTClient.  This program checks to see if the Virtual Server "test vs" is enabled and if it is, it disables it and if it is disabled, it enables it.  A GET is done to retrieve the configuration data for the Virtual Server and the "enabled" value in the "basic" properties section is checked.  This is a boolean value, so if it is true it is set to false and if it is false it is set to true. The changed data is then sent to the server using a PUT.

 

startstopvs.php

 

<?php

# Start or stop a virtual server

require_once 'stmrestclient.inc';

$resource = 'vservers/test vs';

print("<html><body>\n");

try {

    # Set up the connection

    $client = new STMRestClient();


    # Get the config data for the virtual server

    $vsConfig = $client->get($resource);

    if ($vsConfig->properties->basic->enabled) {

        $vsConfig->properties->basic->enabled = false;

        print("test vs is enabled.  Disable it<br>\n");

    } else {

        $vsConfig->properties->basic->enabled = true;

        print("test vs is Diabled.  Enable it<br>\n");

    }

    $response = $client->put($resource, $vsConfig);

} catch (STMConnectException $e) {

    print ("Error connecting to the REST API: " . $e->getMessage());

} catch (STMAuthException $e) {

    print ("Error logging into the REST API: " . $e->getMessage());   

} catch (exception $e) {

    print ('Error: ' . $e->getMessage());

}

print("</body></html>\n");

?>

 

Running the example

 

This code was tested with PHP 5.3.3 and uses the STMRestClient class that can be found here: Tech Tip: A Stingray Traffic Manager REST Client for PHP

 

To run this program, Make it available via a web server and the output should be:

 

test vs is enabled. Disable it.

 

or


test vs is disabled. Enable it.

 

 

Read More

 

Version history
Revision #:
1 of 1
Last update:
‎02-06-2014 08:53:PM
Updated by:
 
Labels (1)