API - knihovna (jen pro Fakturačku Premium)

<?php

class Fakturacka {

    public function __construct(private $email, private $password)
    {
    }

    public function getInvoices(): mixed {
        return $this->run('invoices');
    }
    
    // direction: customer, supplier
    public function getInvoice($direction, $id): mixed {
        return $this->run('get-invoice/' . $direction . '/' . $id);
    }

    public function createInvoice($data): mixed {
        return $this->run('create-invoice', $data);
    }

    private function run(string $path, $data = null): mixed {
        $ch = curl_init('https://fakturacka.cz/API/' . $path);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, [
            'username' => $this->email,
            'password' => $this->password,
            'data' => json_encode($data)
        ]);
        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if ($info['http_code'] >= 400) {
            throw new Exception($response);
        }
        return json_decode($response);
    }

}