vADC Docs

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

by ricknelson on ‎02-06-2014 05:35 PM - edited on ‎05-29-2015 09:27 AM by pwallace_1 (769 Views)

The following code uses Stingray's RESTful API to list all the pools defined on a cluster. The code is written in PHP and uses STMRESTClient. This example has more extensive comments then the following examples and most of these are applicable to all the examples. This program does a single GET request for the list of pools and then loops through that list, each element of which is an associative array, and then outputs the pool name.

 

listpools.php

 

<?php

# Display a list of pools

require_once 'stmrestclient.inc';

print("<html><body>\n<br><b>Pools:</b><br><br>\n");

try {

    # Setup the connection to the REST API

    $client = new STMRestClient();

    # Do the HTTP GET to get the lists of pools

    $response = $client->get('pools');

    # Get the list of pools from the response as an object

    $pools = $response->children;

    # Loop through the list of pools

    foreach ($pools as $pool) {

        print($pool->name . "<br>\n");

    }

} catch (STMConnectException $e) {

    /* We weren't able to connect to the Stingray Traffic Manager REST API

       The most likely reasons for this are:

       - the hostname of the Stingray instance is incorrect

       - this client doesn't have network access to the Stingray instance or port 9070

       - the RESTful API is disabled

       - the RESTful API is using using a different port */

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

} catch (STMAuthException $e) {

    /* We were able to connect to the Stingray Traffic Manager REST API but could not log in

       The user name or password were incorrect.  If they weren't specified then the default

       values are being used and may need to be changed or the user name and password can

       be specified */

    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:

 

Pools:

Pool1

Pool2

 

Read More

 

Contributors