Showing results for 
Search instead for 
Did you mean: 

Tech Tip: A Traffic Manager REST Client for PHP

When using PHP to access the Traffic Manager (STM) RESTful API you can choose to interact directly with the API using cURL, or you can use one of the PHP REST Clients that are available.  I decided to create my own PHP REST Client, STMRESTClient, and instructions for using STMRESTClient are included in this article and the code is attached.




STMRestClient is a PHP class written specifically to work with the STM RESTful API.  So for example, it handles JSON conversions, authorization and understands the path hierarchy of the API.  The attached code has extensive comments that contain more detailed information then is included in this article.  The class contains default values for the hostnameSmiley Tongueort, user name, password and API version so the class can be edited to change these default values.  Any of these values can also be set when the class is instantiated by passing in an associative array with one or more of:


'STMHost' => '<host>:<port>'

'UserId' => '<user name>,

'Password' => '<password>'

'APIVersion' => '<version>'


The default value will be used for any of the values not specified.  So for example, all of the following are correct:


$client = new STMRestClient()

$client = new STMRESTClient(array('UserId' => 'user', 'Password' => 'password');

$client = new STMRESTClient(array('STMHost' => '', 'UserId' => 'user',

                                  'Password' => 'password', 'APIVersion' => '1.1'));


Once the class has been instantiated, any of the class methods can be invoked.  The following three methods are likely to be the most commonly used:


get: This method is used to get data for a resource.

It takes two input parameters:

resource: The end of the URI path for the resource.  So for example, to get the list of pools this would be "pools" and to get data for the pool, "testpool", it would be "pools/testpool".

resource type: This parameter is optional.  It specifies the type of resource and can be one of the following constants:

TYPE_CONFIG: causes the path to be /api/tm/<version>/config/active/<resource>

TYPE_INFO: causes the path to be /api/tm/<version>/status/local_tm/information/<resource>

TYPE_STATS: causes the path to be /api/tm/<version>/status/local_tm/statistics/<resource>

The default is TYPE_CONFIG


For configuration resources, an object will be returned that has been created by decoding the JSON response.  For file resources, the file text will be returned.




$pools = $client->get("pools");

$poolStats = $client->get("pools/testpool", TYPE_STATS);


put: This method is used to add or change a configuration resource.

It takes two input parameters:

resource: The end of the URI path for the resource.

resource data&colon; An object or an associative array containing the resource data or file text.  For configuration resources, the data will be JSON encoded by STMRestClient.

content type: The Content-Type header to use for this data.  This should be "application/octet-stream" for files and

                     "application/json" for all other resources.  It defaults to "application/json".


For configuration resources, the new or changed resource is returned as an object.  For files, nothing is returned.




$vsData->properties->basic->pool = 'testpool';

$vsData->properties->basic->port = '80';

$newVS = $client->put("virtual_servers/newvs", $vsData);


$poolData->properties->basic->nodes = array('');

$poolData = array('properties' => array('basic' => array('nodes' =>



$client->put("rules/newrule", "#test rule", "application/octet-stream");


delete: This method is used to delete a configuration resource.

It takes one input parameter:

resource: The end of the URI for the resource.


It returns true if the resource was deleted.






All three of these methods return the following exceptions:


STMAuthException: If there was an error logging into the STM

STMConnectException: If there was error connecting to the STM

STMResourceNotFoundException: If the resource requested is not found

Exception: Any other errors


There are other methods that can be accessed and there are properties that can be set or retrieved and these are all documented in the class code.  The two public properties that are available are:


returnedContentType: This is the ContentType header returned by the last REST API request

statusCode: This is the HTTP status code returned on the last REST API request


Read More


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