source: trunk/Modules/FinanceBankAPI/FioAPI.php

Last change on this file was 911, checked in by chronos, 3 years ago
  • Modified: Record also remote signal for wireless clients.
File size: 4.7 KB
Line 
1<?php
2
3// Specifikace API: https://www.fio.cz/docs/cz/API_Bankovnictvi.pdf
4
5include('GPC.php');
6
7function RemoveComma(string $Text): string
8{
9 if ((mb_strlen($Text) >= 2) and ($Text[0] == '"') and (mb_substr($Text, -1, 1) == '"')) return mb_substr($Text, 1, -1);
10 else return $Text;
11}
12
13class FioAPI
14{
15 public string $Token;
16 public string $Encoding;
17 public string $Format;
18
19 function __construct()
20 {
21 $this->Encoding = 'utf-8';
22 $this->Format = 'csv';
23 }
24
25 function Import(int $TimeFrom, int $TimeTo): array
26 {
27 if ($this->Token == '') throw new Exception('Missing value for Token property.');
28
29 // URL format: https://www.fio.cz/ib_api/rest/periods/{token}/{datum od}/{datum do}/transactions.{format}
30 // Send request
31 $RequestURL = '/ib_api/rest/periods/'.$this->Token.'/'.
32 date('Y-m-d', $TimeFrom).'/'.date('Y-m-d', $TimeTo).'/transactions.'.$this->Format;
33 $Response = '';
34 $Response = @file_get_contents('https://www.fio.cz'.$RequestURL);
35 if ($Response == FALSE)
36 {
37 throw new Exception('Connection error');
38 } else
39 {
40 if ($this->Format == 'gpc') $Response = iconv('windows-1250', $this->Encoding, $Response);
41 $Response = explode("\n", $Response);
42
43 if ($this->Format == 'gpc')
44 {
45 // Parse all GPC lines
46 $GPC = new GPC();
47 $Result = array();
48 foreach ($Response as $Index => $Line)
49 {
50 if (($Index == 0) and (substr($Line, 0, strlen(GPC_TYPE_REPORT)) != GPC_TYPE_REPORT)) $this->NoValidDataError($Response);
51 $GPCLine = $GPC->ParseLine($Line);
52 if ($GPCLine != NULL) $Result[] = $GPCLine;
53 }
54 } else
55 if ($this->Format == 'csv')
56 {
57 $Result = array(
58 'Items' => array(),
59 );
60
61 // CVS header
62 while ((count($Response) > 0) and ($Response[0] != ''))
63 {
64 $Line = explode(';', $Response[0]);
65 if ($Line[0] == 'accountId') $Result['AccountNumber'] = $Line[0];
66 else if ($Line[0] == 'bankId') $Result['BankId'] = $Line[0];
67 else if ($Line[0] == 'currency') $Result['Currency'] = $Line[0];
68 else if ($Line[0] == 'iban') $Result['IBAN'] = $Line[0];
69 else if ($Line[0] == 'bic') $Result['BIC'] = $Line[0];
70 else if ($Line[0] == 'openingBalance') $Result['OpeningBalance'] = $Line[0];
71 else if ($Line[0] == 'closingBalance') $Result['ClosingBalance'] = $Line[0];
72 else if ($Line[0] == 'dateStart') $Result['DateStart'] = $Line[0];
73 else if ($Line[0] == 'dateEnd') $Result['DateEnd'] = $Line[0];
74 else if ($Line[0] == 'idFrom') $Result['IdFrom'] = $Line[0];
75 else if ($Line[0] == 'idTo') $Result['IdTo'] = $Line[0];
76 array_shift($Response);
77 }
78 array_shift($Response); // Remove empty line
79
80 if ((count($Response) == 0) or
81 ($Response[0] != 'ID pohybu;Datum;Objem;Měna;Protiúčet;Název protiúčtu;Kód banky;Název banky;KS;VS;SS;Uživatelská identifikace;Zpráva pro příjemce;Typ;Provedl;Upřesnění;Komentář;BIC;ID pokynu')
82 ) throw new Exception('Unsupported CSV header');
83 array_shift($Response);
84 array_pop($Response);
85 foreach ($Response as $Index => $Line)
86 {
87 $Line = explode(';', $Line);
88 $Date = explode('.', $Line[1]);
89 $Date = mktime(0, 0, 0, $Date[1], $Date[0], $Date[2]);
90 $NewRecord = array('ID' => $Line[0], 'Date' => $Date, 'Value' => $Line[2], 'CurrencyCode' => $Line[3],
91 'OffsetAccount' => $Line[4], 'OffsetAccountName' => $Line[5], 'BankCode' => $Line[6], 'BankName' => RemoveComma($Line[7]),
92 'ConstantSymbol' => $Line[8], 'VariableSymbol' => $Line[9], 'SpecificSymbol' => $Line[10],
93 'UserIdent' => RemoveComma($Line[11]), 'Message' => RemoveComma($Line[12]), 'Type' => RemoveComma($Line[13]),
94 'User' => RemoveComma($Line[14]), 'Details' => RemoveComma($Line[15]), 'Comment' => RemoveComma($Line[16]),
95 'BIC' => $Line[17], 'OrderID' => $Line[18]);
96 $Result['Items'][] = $NewRecord;
97 }
98 }
99 return $Result;
100 }
101 }
102
103 function NoValidDataError(array $Response): void
104 {
105 // Try to get error message
106 // If something go wrong fio show HTML login page and display error message
107 $Response = implode('', $Response);
108 $ErrorMessageStart = '<div id="oldform_warning">';
109 if (strpos($Response, $ErrorMessageStart) !== false)
110 {
111 $Response = substr($Response, strpos($Response, $ErrorMessageStart) + strlen($ErrorMessageStart));
112 $ErrorMessage = trim(substr($Response, 0, strpos($Response, '</div>')));
113 } else $ErrorMessage = '';
114 throw new Exception('No valid GPC data: '.$ErrorMessage);
115 }
116}
Note: See TracBrowser for help on using the repository browser.