Enterprise power with human face

Saturday, October 20, 2012

Calling TM1 Processes From PHP via TM1RunTI.exe

In the previous post I explained how to call TM1 processes from inside TM1RunTI.exe native utility. However, this function solitarily is still not useful and for purpose of universal API needs to be wrapped in some server side code. Today I will therefore focus on building general PHP functions executeProcess for invoking TM1 processes and subsequently echoing their outputs to the browser interface via getData.  Whereas both functions will be necessary for every class, they will be stored in api.php file in root folder of api project code. Code snippet is attached bellow.

executeProcess

PHP allows executing external programs via system function with two parameters (line 21). First parameter provides particular command similar to one issued directly tonative utility however properly formatted.  For the purpose of its universality I also replaced parameters with variables allowing function to be repeatedly re-used in different objects and for various processes. Second parameter message stores received response, it case of TM1RunTI.exe restricted to set of numerical codes (0-11,99). For their better readability, I encoded them into error array (line 4).   

getData

Function is supposed to be called immediately after executeProcess is finished when it reads exported file, completes JSON formatting and outputs data it to browser window. The required parameter is file location with path where data is stored. As you can see, the only modification done within the function is enveloping loaded string with angle brackets (line 37 & 41). Function returns JSON formatted output (line 42).



Imagine now, that you want to build function which returns data from Bears cube after invoking http://localhost:6600/analyticshumanly/api/cube/bears.php with GET method. The topic how to create pretty URLs, structure folders and handling naming conventions will be discussed in the following post. For now, it is sufficient to know that while the notion of the function is to get data from Bears cube, the code will be stored in api/cube/bears.php.

Requested cube Bears

Within the bears.php code, we have to firstly import out universal api.php library by calling require function (line 4). Further export file location, adminhost, server, user and password needs to be declared. Notice, that we haven’t determined particular process name yet. That is because the process called within Bears cube will depend on method which called the URL. 
Method can be identify by $_SERVER[‘REQUEST_METHOD’]. Invoked executeRequest function then switches through methods and calls particular processes. When message code is different to zero, proper warning from error array is issued (line 56), otherwise, data from the file are loaded and echoed to the browser (line 59). Prior to the request execution, we clear the content of the file by calling clearFile function (line 26). Code for bears.php is attached in the following snippet.



When you type http://localhost:6600/analyticshumanly/api/cube/bears.php to your borwser now, you can see that API responded with JSON formated array of nine entries from Bears cube.

JSON formatted responses with data from Bears cube

In the following article, I will explain how to invoke API, provide method and call specific URL from web browser.

No comments:

Post a Comment