vADC Docs

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

by ricknelson on ‎02-06-2014 09:50 PM - edited on ‎05-29-2015 09:52 AM by pwallace_1 (455 Views)

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.

 

addextrafile.

 

<?php

require_once 'stmrestclient.inc';

$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

 

or

 

File validserialnumbers replaced

 

Notes

 

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

 

Contributors