Changeset 550 for trunk/Modules/FinanceBankAPI/FioAPI.php
- Timestamp:
- Jul 7, 2013, 7:37:48 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/FinanceBankAPI/FioAPI.php
r548 r550 4 4 5 5 include('GPC.php'); 6 7 function RemoveComma($Text) 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 } 6 12 7 13 class FioAPI … … 9 15 var $Token; 10 16 var $Encoding; 17 var $Format; 11 18 12 19 function __construct() 13 20 { 14 21 $this->Encoding = 'utf-8'; 22 $this->Format = 'csv'; 15 23 } 16 24 … … 20 28 21 29 // URL format: https://www.fio.cz/ib_api/rest/periods/{token}/{datum od}/{datum do}/transactions.{format} 22 echo('1'); 23 $fp = fsockopen('ssl://www.fio.cz', 443, $errno, $errstr, 30); 24 echo('2'); 25 if(!$fp) 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 echo($RequestURL); 34 $Response = file_get_contents('https://www.fio.cz'.$RequestURL); 35 if($Response == FALSE) 26 36 { 27 throw new Exception('Connection error : '.$errstr);37 throw new Exception('Connection error'); 28 38 } else 29 39 { 30 // Send request 31 $RequestURL = '/ib_api/rest/periods/'.$this->Token.'/'. 32 date('Y-m-d', $TimeFrom).'/'.date('Y-m-d', $TimeTo).'/transactions.gpc'; 33 $Request = "GET ".$RequestURL." HTTP/1.1\r\n"; 34 $Request .= "Host: www.fio.cz\r\n"; 35 $Request .= "User-Agent: PHP Script\r\n"; 36 $Request .= "Content-Type: text/html\r\n"; 37 $Request .= "Connection: Close\r\n\r\n"; 38 fwrite($fp, $Request); 39 40 // Read response 41 echo('3'); 42 $Response = array(); 43 while(!feof($fp)) 44 $Response .= trim(fgets($fp, 1024))."\n"; 45 fclose($fp); 46 echo('4'); 47 $Response = iconv('windows-1250', $this->Encoding, $Response); 40 echo($Response); 41 if($this->Format == 'gpc') $Response = iconv('windows-1250', $this->Encoding, $Response); 48 42 $Response = explode("\n", $Response); 49 50 // Strip HTTP header 51 while($Response[0] != '') array_shift($Response); 52 array_shift($Response); // Remove empty line 53 //echo(implode("\n", $Response)); 54 55 // Parse all GPC lines 56 $GPC = new GPC(); 57 $Result = array(); 58 foreach($Response as $Index => $Line) 43 44 if($this->Format == 'gpc') 45 { 46 // Parse all GPC lines 47 $GPC = new GPC(); 48 $Result = array(); 49 foreach($Response as $Index => $Line) 50 { 51 if(($Index == 0) and (substr($Line, 0, strlen(GPC_TYPE_REPORT)) != GPC_TYPE_REPORT)) $this->NoValidDataError($Response); 52 $GPCLine = $GPC->ParseLine($Line); 53 if($GPCLine != NULL) $Result[] = $GPCLine; 54 } 55 } else 56 if($this->Format == 'csv') 59 57 { 60 if(($Index == 0) and (substr($Line, 0, strlen(GPC_TYPE_REPORT)) != GPC_TYPE_REPORT)) $this->NoValidDataError($Response); 61 $GPCLine = $GPC->ParseLine($Line); 62 if($GPCLine != NULL) $Result[] = $GPCLine; 58 $Result = array( 59 'Items' => array(), 60 ); 61 62 // CVS header 63 while($Response[0] != '') 64 { 65 $Line = explode(';', $Response[0]); 66 if($Line[0] == 'accountId') $Result['AccountNumber'] = $Line[0]; 67 else if($Line[0] == 'bankId') $Result['BankId'] = $Line[0]; 68 else if($Line[0] == 'currency') $Result['Currency'] = $Line[0]; 69 else if($Line[0] == 'iban') $Result['IBAN'] = $Line[0]; 70 else if($Line[0] == 'bic') $Result['BIC'] = $Line[0]; 71 else if($Line[0] == 'openingBalance') $Result['OpeningBalance'] = $Line[0]; 72 else if($Line[0] == 'closingBalance') $Result['ClosingBalance'] = $Line[0]; 73 else if($Line[0] == 'dateStart') $Result['DateStart'] = $Line[0]; 74 else if($Line[0] == 'dateEnd') $Result['DateEnd'] = $Line[0]; 75 else if($Line[0] == 'idFrom') $Result['IdFrom'] = $Line[0]; 76 else if($Line[0] == 'idTo') $Result['IdTo'] = $Line[0]; 77 array_shift($Response); 78 } 79 array_shift($Response); // Remove empty line 80 81 if($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 } 63 98 } 64 99 return($Result);
Note:
See TracChangeset
for help on using the changeset viewer.