This article discusses how to use the Stingray Traffic Manager's RESTful Control API with PHP. The are different options for accessing the RESTful API with PHP, but I decided to create my own PHP REST Client, STMRESTClient, to make working with RESTful API easier, and this has been used in the PHP examples. Instructions for using STMRESTClient and the code can be found here: Tech Tip: A Stingray Traffic Manager REST Client for PHP
The RESTful API gives you access to the Stingray Configuration and statistics, presented in the form of resources. The format of the data exchanged using the Stingray RESTful API will depend on the type of resource being accessed:
Working with JSON and PHP
PHP provides functions for JSON encoding and decoding. To take a PHP data structure and encode it into JSON format, use json_encode() and to decode a JSON formatted string into a PHP structure, use json_decode(). If using STMRestClient, JSON encoding and decoding will be done for you.
Working with the RESTful API and PHP
The base form of the URI for the Stingray RESTful API is:
https://<host>:<port>/api/tm/<version>
followed by paths to the different resource types:
"local_tm" can be used in place of <host>
followed by a actual resource, so for example to get a list of all the pools from the Stingray instance, stingray.example.com, it would be:
https://stingray.example.com:9070/api/tm/2.0/config/active/pools
and to get the configuration information for the pool, “testpool” it would be:
https://stingray.example.com:9070/api/tm/2.0/config/active/pools/testpool
and to get statistics for the pool "testpool", it would be:
https://stingray.example.com:9070/api/tm/2.0/status/local_tm/statistics/pools/testpool
If your PHP environment does not have cURL installed, you will need to install it, even if you are using STMRestClient.
If using apt (assuming apache is the web server):
sudo apt-get install php5-curl
sudo service apache2 restart
The PHP functions json_decode and json_encode convert between JSON strings and PHP data structures. The PHP data structure can be either an associative array or an object. In either case, the array or object will have one element. The key to this element will be:
Please see Feature Brief: Traffic Manager's RESTful Control API for examples of these data structures and something like the Chrome REST Console can be used to see what the actual data looks like.