API - knihovna (jen pro Fakturačku Premium)
<?php
class Fakturacka {
private $email;
private $password;
public function __construct($email, $password) {
$this->email = $email;
$this->password = $password;
}
public function getInvoices() {
return $this->run('invoices');
}
// direction: customer, supplier
public function getInvoice($direction, $id) {
return $this->run('get-invoice/' . $direction . '/' . $id);
}
public function createInvoice($data) {
return $this->run('create-invoice', $data);
}
private function run($path, $data = null) {
$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);
}
}