From d54f15be2a1eef5a1ae79602c5af4df63a9e0299 Mon Sep 17 00:00:00 2001 From: Sebastian Nohn Date: Sun, 26 Nov 2017 23:42:12 +0100 Subject: [PATCH] add example for USG traffic statistics (#3) * add example for USG traffic statistics * fix bps --- examples/usg-traffic-stats.php | 39 ++++++++++++++++++++++++++++++++++ src/Client.php | 11 ++++++++++ 2 files changed, 50 insertions(+) create mode 100644 examples/usg-traffic-stats.php diff --git a/examples/usg-traffic-stats.php b/examples/usg-traffic-stats.php new file mode 100644 index 0000000..a01f64b --- /dev/null +++ b/examples/usg-traffic-stats.php @@ -0,0 +1,39 @@ + $config['base_uri']])); + +try { + + // login to the unifi controller API + $apiClient->login($config['username'], $config['password']); + + // Fetch statistics for the given site + $responseBody = $apiClient + ->health($config['site']) + ->getBody() + ->getContents(); + + $data = json_decode($responseBody, true); + echo "Latency: ".$data['data'][2]['latency']." ms\n"; + echo "RX : ".round(($data['data'][2]['rx_bytes-r']/1024/1024*8), 2)." Mb/s\n"; + echo "TX : ".round(($data['data'][2]['tx_bytes-r']/1024/1024*8), 2)." Mb/s\n"; + $apiClient->logout(); + +} catch (RequestException $e) { + echo $e->getMessage() . PHP_EOL; + + echo '----- Request ------' . PHP_EOL; + echo $e->getRequest()->getBody()->getContents(); + echo PHP_EOL; + + echo '----- Response ------' . PHP_EOL; + echo $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : '- no response -'; + echo PHP_EOL; +} \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index c1d6380..135fc9f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -90,6 +90,17 @@ class Client $this->client->request('get', '/logout', ['allow_redirects' => false] + $this->requestOptions); } + /** + * @param string $site + * + * @return ResponseInterface + * @throws GuzzleException + */ + public function health($site) + { + return $this->get('/api/s/' . $site . '/stat/health'); + } + /** * @param string $site *