Mick
Mick I'm one of the co-founders of WebTuna Software.

WebTuna REST API – simple PHP example

WebTuna REST API – simple PHP example

The PHP code below is a simple example of how to query the WebTuna REST API to get metric data programmatically. In this example it looks at the last 300 seconds (5 mins) of data and requests /rest/v1/activity/summary where type=page as described here.

This returns JSON object with a single set of aggregated values for things like average response time and number of hits. The JSON response will look something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"unused" : "",
"users" : 33,
"hits" : 128,
"sum_responsestart" : 54805.0,
"sum_responsetime" : 497962.0,
"avg_responsetime" : 3890,
"min_responsetime" : 7,
"max_responsetime" : 19989,
"stddev_responsetime" : 4415,
"sum_elements" : 6786.0,
"min_elements" : 4,
"max_elements" : 95,
"sum_pagesize" : 0.0,
"apdex" : 0.8700000047683716
}
]

In this example we are just choosing the avg_responsetime and printing it to the screen.

#!/usr/bin/php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
    // Setup the input Parameters
    date_default_timezone_set('Europe/London');
    $to = date('Y-m-d+H:i:s', time());
    $from = date('Y-m-d+H:i:s',time() - 300);
    // Setup the HTTP call to the REST API
    $service_url = "https://my.webtuna.com/rest/v1/activity/summary?from=$from&to=$to&type=page";
    $curl = curl_init($service_url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // Set the Authorization and other HTTP headers $username = 'mickmcg'; $api_key = '269d5677d84d2d1f3241bcbf8f2020bbe802a8c2';
    $accesstoken = base64_encode($username . ":" . $api_key);
    $headers = array();
    $headers[] = 'Content-length: 0';
    $headers[] = 'Content-type: application/json';
    $headers[] = 'Authorization: Basic '.$accesstoken; curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    // Make the HTTP call $curl_response = curl_exec($curl);
    if ($curl_response === false)
        { $info = curl_getinfo($curl);
            curl_close($curl); 
            die('error occured during curl exec. Additional info: ' . var_export($info)); 
        } 
            curl_close($curl);
            // Decode the JSON reponse print_r($curl_response);
            $decoded_obj = json_decode($curl_response);
            print $decoded_obj[0]->{'avg_responsetime'};
?>

More information on the use of the REST API can be found here https://docs.webtuna.com/rest