Showing results for 
Search instead for 
Did you mean: 

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

The following code uses Stingray's RESTful API to add a pool.   The code is written in PHP and uses STMRESTClient. This program creates a new pool, "phptest", first doing a GET to make sure the pool doesn't already exist, and if the pool doesn't exist, the pool is created by doing a PUT with just the minimum data needed to create a pool.  In this case the program creates a properties object with just one node.  All other values will get default values when Stingray creates the pool.





# Add a pool

require_once '';

$poolName = 'phptest';

$node = '';

try {

    # Set up the connection

    $client = new STMRestClient();

    /* Check to see if the pool already exist.  If it does not yet exist, then a

     * ResourceNotFoundException exception will be thrown. */

    $response = $client->get("pools/$poolName");

    print("Pool $poolName already exists");

} catch (STMResourceNotFoundException $e) {

    # The pool doesn't exist, so it can be created

    $poolConfig->properties->basic->nodes = array($node);

    #$poolConfig = array('properties' => array('basic' => array('nodes' => array($node))));

    try {

        $response = $client->put("pools/$poolName", $poolConfig);

        print("Pool $poolName added");

    } catch (Exception $e) {

        print ("Error creating Pool $poolName: " . $e->getMessage());


} 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());





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:


Pool phptest added




The only difference between doing a PUT to change a resource and a PUT to add a resource is the HTTP status code returned.  When changing a resource 200 is the expected status code and when adding a resource, 201 is the expected status code.


Read More



Version history
Revision #:
2 of 2
Last update:
‎01-06-2021 04:17:PM
Updated by:
Labels (1)