├── composer.json ├── README.md ├── LICENSE └── src └── Ofx.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "asmpkg/ofx", 3 | "description": "Transforma em objetos dados de um arquivo de extrato bancario OFX.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Alex Monte", 8 | "email": "alexsmonte@gmail.com" 9 | } 10 | ], 11 | "require": {"php": ">=5.3"}, 12 | "autoload": { 13 | "psr-4": { 14 | "Asmpkg\\Ofx\\": "src" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ofx 2 | Transforma em objetos dados de um arquivo de extrato bancario OFX. 3 | 4 | este codigo foi retirado do forum https://forum.imasters.com.br/topic/490216-leitura-de-arquivos-ofx-com-php/ 5 | 6 | # Instalação 7 | composer require asmpkg/ofx 8 | 9 | use Asmpkg/Ofx; 10 | 11 | $ofx = new Ofx("Arquivo.ofx"); 12 | 13 | Data inicio e fim 14 | $ofx->dtStar 15 | $ofx->dtEnd 16 | 17 | Codigo do Banco 18 | $ofx->bankId 19 | 20 | Conta do banco 21 | $ofx->acctId 22 | 23 | Nome do banco 24 | $ofx->org 25 | 26 | Extrato 27 | foreach($ofx->bankTranList as $extrato) 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Alex Monte 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Ofx.php: -------------------------------------------------------------------------------- 1 | arquivo = $arquivo; 25 | 26 | return $this->retorno(); 27 | } 28 | 29 | public function converterOfxEmXml() 30 | { 31 | 32 | $content = utf8_decode(file_get_contents($this->arquivo)); 33 | $line = strpos($content, ""); 34 | $ofx = substr($content, $line - 1); 35 | $buffer = $ofx; 36 | $count = 0; 37 | 38 | while ($pos = strpos($buffer, '<')) 39 | { 40 | $count++; $pos2 = strpos($buffer, '>'); 41 | $element = substr($buffer, $pos + 1, $pos2 - $pos - 1); 42 | 43 | if (substr($element, 0, 1) == '/') 44 | $sla[] = substr($element, 1); 45 | else $als[] = $element; 46 | $buffer = substr($buffer, $pos2 + 1); 47 | } 48 | $adif = array_diff($als, $sla); 49 | 50 | 51 | $adif = array_unique($adif); 52 | $ofxy = $ofx; 53 | 54 | foreach ($adif as $dif) 55 | { 56 | $dpos = 0; 57 | while ($dpos = strpos($ofxy, $dif, $dpos + 1)) 58 | { 59 | $npos = strpos($ofxy, '<', $dpos + 1); 60 | echo $dif."
"; 61 | $ofxy = substr_replace($ofxy, "".chr(10)."<", $npos, 1); 62 | $dpos = $npos + strlen($element) + 3; 63 | } 64 | } 65 | $ofxy = str_replace('&', '&', $ofxy); 66 | 67 | //return new SimpleXMLElement($ofxy); 68 | 69 | return $ofxy; 70 | } 71 | 72 | 73 | public function closeTags($ofx=null) { 74 | $buffer = ''; 75 | $source = fopen($ofx, 'r') or die("Unable to open file!"); 76 | while(!feof($source)) { 77 | $line = trim(fgets($source)); 78 | if ($line === '') continue; 79 | 80 | if (substr($line, -1, 1) !== '>') { 81 | list($tag) = explode('>', $line, 2); 82 | $line .= ''; 83 | } 84 | $buffer .= $line ."\n"; 85 | } 86 | 87 | 88 | $xmlOut = explode("", $buffer); 89 | 90 | //$name = realpath(dirname($ofx)) . '/' . date('Ymd') . '.ofx'; 91 | //$file = fopen($name, "w") or die("Unable to open file!"); 92 | //fwrite($file, $buffer); 93 | //fclose($file); 94 | 95 | return isset($xmlOut[1])?"".$xmlOut[1]:$buffer; 96 | } 97 | 98 | public function retorno() 99 | { 100 | $retorno = new SimpleXMLElement(utf8_encode($this->closeTags($this->arquivo))); 101 | 102 | $this->bankTranList = $retorno->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->STMTTRN; 103 | $this->dtStar = $retorno->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->DTSTART; 104 | $this->dtEnd = $retorno->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->DTEND; 105 | 106 | $this->org = $retorno->SIGNONMSGSRSV1->SONRS->FI->ORG; 107 | $this->acctId = $retorno->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKACCTFROM->ACCTID; 108 | $this->bankId = $retorno->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKACCTFROM->BANKID; 109 | 110 | return $this; 111 | } 112 | 113 | } 114 | --------------------------------------------------------------------------------