Showing results for 
Search instead for 
Did you mean: 

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

The following code uses Stingray's RESTful API to a file to the extra directory.   The code is written in PHP and uses STMRESTClient.  This program adds the file 'validserialnumbers' to the extra directory and if the file already exists it will be overwrite it.  If this is not the desired behavior, code can be added to to check for the existence of the file as was done in the addpool example.





require_once '';

$fileName = 'validserialnumbers';

$validSerialNumbers = "123456\n234567\n345678\n";

try {

    # Set up the connection

    $client = new STMRestClient();

    $client->put("extra/$fileName", $validSerialNumbers, 'application/octet-stream');

    # If the file already exists, it will be replaced with this version and 204 will be returned

    # otherwise 201 will be returned.

    print("<br>statusCode=" . $client->statusCode . "<br><br>");

    if ($client->statusCode == 201) {

        print("File $fileName added");

    } else {

        print("File $fileName replaced");


} 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:


File validserialnumbers added




File validserialnumbers replaced




Since this is a file and not a configuration resource, JSON will not be used and the MIME type will be "application/octet-stream".  Another difference when dealing with files is how Stingray handles adding a file that already exists.  If the file already exists, Stingray will overwrite the it and return a HTTP status code of 204.  If the file doesn't already exist, the HTTP status code will be a 201.


Read More



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