Segue Classe:
<?php
/*namespace Asmpkg\Ofx;
use SimpleXMLElement;
*/
/***
este codigo foi retirado do forum https://forum.imasters.com.br/topic/490216-leitura-de-arquivos-ofx-com-php/
e corrigido falhas e adaptado as minhas necessidades
Haroldo/2012
***/
class Ofx
{
private $arquivo;
public $bankTranList;
public $dtStar;
public $dtEnd;
public $bankId;
public $acctId;
public $org;
public $balance;
public function __construct($arquivo)
{
$this->arquivo = $arquivo;
return $this->retorno();
}
public function converterOfxEmXml()
{
$content = $this->closeTags($this->arquivo);
$line = strpos($content, "<OFX>");
$ofx = substr($content, $line - 1);
$buffer = $ofx;
$count = 0;
while ($pos = strpos($buffer, '<'))
{
$count++;
$pos2 = strpos($buffer, '>');
$element = substr($buffer, $pos + 1, $pos2 - $pos - 1);
if (substr($element, 0, 1) == '/')
$sla[] = substr($element, 1);
else $als[] = $element;
$buffer = substr($buffer, $pos2 + 1);
}
$adif = array_diff($als, $sla);
$adif = array_unique($adif);
$ofxy = $ofx;
foreach ($adif as $dif)
{
$dpos = 0;
while ($dpos = strpos($ofxy, $dif, $dpos + 1))
{
$npos = strpos($ofxy, '<', $dpos + 1);
$ofxy = substr_replace($ofxy, "</$dif>".chr(10)."<", $npos, 1);
$dpos = $npos + strlen($element) + 3;
}
}
$ofxy = str_replace('&', '&', $ofxy);
//return new SimpleXMLElement($ofxy);
return $ofxy;
}
public function closeTags($ofx=null) {
$buffer = '';
$source = fopen($ofx, 'r') or die("Unable to open file!");
while(!feof($source)) {
$line = trim(fgets($source));
if ($line === '') continue;
if (substr($line, -1, 1) !== '>') {
list($tag) = explode('>', $line, 2);
$line .= '</' . substr($tag, 1) . '>';
}
$buffer .= $line; //.chr(10);
}
$xmlOut = explode("<OFX>", $buffer);
return isset($xmlOut[1])?"<OFX>".$xmlOut[1]:$buffer;
}
public function retorno()
{
$xmlString = $this->closeTags($this->arquivo);
$encode = mb_detect_encoding($xmlString);
if ($encode !== 'UTF-8' || !mb_check_encoding($xmlString,"UTF-8")) {
$xmlString = utf8_encode($xmlString);
}
$xml = new SimpleXMLElement($xmlString);
$this->bankTranList = $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->STMTTRN;
$this->dtStar = (string) $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->DTSTART;
$this->dtEnd = (string) $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->DTEND;
$this->org = (string) $xml->SIGNONMSGSRSV1->SONRS->FI->ORG;
$this->acctId = (string) $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKACCTFROM->ACCTID;
$this->bankId = (string) $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKACCTFROM->BANKID;
$this->balance = (float) $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->LEDGERBAL->BALAMT;
$this->balanceDate = (string) $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->LEDGERBAL->DTASOF;
return $this;
}
}
Utilização:
$ofx = new Ofx([iw]->ofxFile);
$_saldo = ['date'=>$ofx->balanceDate, 'balance'=> $ofx->balance];
[iw]->conta = [$ofx->bankId,$ofx->acctId];
[iw]->extrato=[];
[iw]->datas=[];
$_valorMovto = 0;
foreach ($ofx->bankTranList as $transaction) {
$_data = date("d/m/Y", strtotime(substr($transaction->DTPOSTED, 0, 8)));
//if (isset($banco[$ofx->bankId]) && $banco[$ofx->bankId]['utf8']) {
//$_desc = (string) $transaction->MEMO;
//}else{
$_desc = (string) utf8_decode($transaction->MEMO);
//}
$_valor = (string) $transaction->TRNAMT;
$_valor = substr($_valor, -2,1) == '.'? $_valor : str_replace(',','.', $_valor);
$_tipo = $_valor >= 0 ?'C':'D';
$_valorMovto += (float) $_valor;
//$_valor = number_format($_valor,2,',','.');
[iw]->extrato[]=[$_data,$_desc,$_tipo,$_valor];
[iw]->datas[$_data] = true;
}
[iw]->saldoAtu = number_format((float)$_saldo['balance'], 2, ',', '.');
[iw]->saldoAnt = number_format((float)$_saldo['balance'] - $_valorMovto, 2, ',', '.');
[iw]->saldoData = date("d/m/Y", strtotime(substr($_saldo['date'], 0, 8)));