Skip to main content

ABRA Flexi API in PHP

Sample API Usage in PHP

Written by Lenka Haringerová

Looking for a sample example of working with the Flexi API in PHP?

Shared Code

To keep the individual examples simpler, we will prepare some shared code that sets up the environment:

// konfigurace serveru 
$host = "https://demo.flexibee.eu";
$firma = "demo";

$ch = curl_init(); // create curl resource
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // return content as a string from curl_exec
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // follow redirects (compatibility for future changes in Flexi)
curl_setopt($ch, CURLOPT_HTTPAUTH, TRUE); // HTTP authentication curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Flexi by default uses Self-Signed certificates
// curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
// For debugging curl_setopt($ch, CURLOPT_USERPWD, "winstrom:winstrom"); // set username and password

And optionally display errors:

$output = curl_exec($ch); 

$info = curl_getinfo($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200 && curl_getinfo($ch, CURLINFO_HTTP_CODE) != 201)
{
printf ("Při operaci nastala chyba (HTTP %d): %sn", curl_getinfo($ch, CURLINFO_HTTP_CODE), $output);
}

// close curl resource to free up system resources
curl_close($ch);

Creating a Document

You can create a document by submitting a simple XML (in Flexi XML format), which will handle the creation of the document.

// Požadavek odešleme ve formátu XML 

curl_setopt($ch, CURLOPT_URL, $host."/c/".$firma."/faktura-vydana.xml");

// Nastavení samotné operace
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

// Nastavení parametrů pro založení jednoduché faktury
curl_setopt($ch, CURLOPT_POSTFIELDS, '
<winstrom version="1.0">
<faktura-vydana>
<typDokl>code:FAKTURA</typDokl>
<firma>code:WINSTROM</firma>
<popis>Moje faktura v PHP</popis>
<sumZklZakl>1000.0</sumZklZakl>
<bezPolozek>true</bezPolozek>
</faktura-vydana>
</winstrom>
');


// Nastavení správné hlavičky
$headers = ['Content-Type: application/json'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers

The identifier of the created invoice is returned in several ways (see supported HTTP operations for more details):

You can do the same in JSON format:

// Požadavek odešleme ve formátu JSON 
curl_setopt($ch, CURLOPT_URL, $host."/c/".$firma."/faktura-vydana.json");

// Nastavení parametrů pro založení jednoduché faktury
// Nastavení samotné operace

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

// Nastavení parametrů pro založení jednoduché faktury

$faktura = array( "winstrom" => array (
"faktura-vydana" => array(
"typDokl" => "code:FAKTURA", "firma" => "code:WINSTROM", "popis" => "Moje Faktura v PHP","sumDphZakl" => "1000.0", "bezPolozek" => "true",
)
)
);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($faktura));

// Nastavení správné hlavičky
$headers = ['Content-Type: application/json'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Exporting an Invoice to ISDOC

You can read a document in many formats including XML, JSON, PDF, or ISDOC (overview of supported formats):

// Požadavek chceme ve formátu ISDOCX 
curl_setopt($ch, CURLOPT_URL, $host."/c/".$firma."/faktura-vydana/19.isdocx");

// Nastavení samotné operace
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

// Proveď samotnou operaci $output = curl_exec($ch);

// Odešleme výsledek do prohlížeče uživatele
header("Content-Type: application/x-isdocx");
print ($output);

Deleting a Document

If you want to delete a document, simply invoke the HTTP DELETE operation on it:

// Nastavení URL. JSON na konci je proto, abychom dostali případnou 
// chybovou odpověď ve formát JSON.
curl_setopt($ch, CURLOPT_URL, $host."/c/".$firma."/faktura-vydana/19.json");

// Nastavení samotné operace
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");

// Proveď samotnou operaci
$output = curl_exec($ch);
Did this answer your question?