Thursday, April 13, 2017

Get sales info with API call.

A customer asked how they could get all sales data from Group-Office on to a television screen. I've written this little script to get this info. Group-Office has no real API (yet) but it is possible to get this data.

The client class is used to connect to Group-Office and get's the year report data which they can use to display.

Because this might be useful to others I've written this tiny blog post:


<?php

class Client {

 private $host;
 private $curl;

 public function __construct($host, $username, $password) {
  $this->curl = curl_init();

  $this->host = rtrim($host, '/');

  curl_setopt($this->curl, CURLOPT_USERPWD, $username . ":" . $password);
  curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
 }

 public function request($route, array $data = array()) {

  curl_setopt($this->curl, CURLOPT_URL, $this->host . '/index.php?r=' . $route);
  curl_setopt($this->curl, CURLOPT_POST, 1);
  curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));

  $json = curl_exec($this->curl);

  return json_decode($json);
 }

 public function __destruct() {
  curl_close($this->curl);
 }

}

$client = new Client('https://example.group-office.com', 'apiuser', 'SECRET');

$data = $client->request('billing/report/yearReport', array(
  'start_date' => '01-01-2017',
  'end_date' => '31-12-2017',
  'books' => json_encode(array('2'))
    )
);

var_dump($data);