PHP code example of fernandocarletti / ofx

1. Go to this page and download the library: Download fernandocarletti/ofx library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fernandocarletti / ofx example snippets


use Ofx\Parser;

$parser = new Parser();
$ofx = $parser->parseFile('/path/to/statement.ofx');

// Access bank statement transactions
$bankMessages = $ofx->bankMessagesResponseV1;
$statement = $bankMessages->statementTransactionResponses[0]->statementResponse;

foreach ($statement->transactionList->transactions as $transaction) {
    echo sprintf(
        "%s: %s %s\n",
        $transaction->datePosted->format('Y-m-d'),
        $transaction->name,
        $transaction->amount
    );
}

// Check balances
echo "Ledger Balance: " . $statement->ledgerBalance->amount . "\n";
echo "Available Balance: " . $statement->availableBalance->amount . "\n";

use Ofx\Parser;

$parser = new Parser();
$ofx = $parser->parseFile('bank_statement.ofx');

$statement = $ofx->bankMessagesResponseV1
    ->statementTransactionResponses[0]
    ->statementResponse;

// Account information
$account = $statement->bankAccount;
echo "Routing: " . $account->routingNumber . "\n";
echo "Account: " . $account->accountId . "\n";
echo "Type: " . $account->accountType->value . "\n"; // CHECKING, SAVINGS, etc.

// Transaction details
foreach ($statement->transactionList->transactions as $txn) {
    echo sprintf(
        "[%s] %s - %s (%s)\n",
        $txn->type->value,           // CREDIT, DEBIT, CHECK, etc.
        $txn->transactionId,
        $txn->name,
        $txn->amount
    );
    
    if ($txn->isDebit()) {
        echo "  This was a debit transaction\n";
    }
    
    if ($txn->checkNumber !== null) {
        echo "  Check #" . $txn->checkNumber . "\n";
    }
}

$ofx = $parser->parseFile('credit_card.ofx');

$statement = $ofx->creditCardMessagesResponseV1
    ->statementTransactionResponses[0]
    ->creditCardStatementResponse;

// Credit card account
echo "Card: " . $statement->creditCardAccount->accountId . "\n";

// Transactions
foreach ($statement->transactionList->transactions as $txn) {
    echo $txn->datePosted->format('M d') . ": ";
    echo $txn->name . " " . $txn->amount . "\n";
}

// Balances
echo "Current Balance: " . $statement->ledgerBalance->amount . "\n";
echo "Available Credit: " . $statement->availableBalance->amount . "\n";

$ofx = $parser->parseFile('investment.ofx');

$statement = $ofx->investmentMessagesResponseV1
    ->statementTransactionResponses[0]
    ->investmentStatementResponse;

// Investment account
$account = $statement->investmentAccount;
echo "Broker ID: " . $account->brokerId . "\n";
echo "Account: " . $account->accountId . "\n";

// Positions
foreach ($statement->positionList->positions as $position) {
    echo sprintf(
        "%s: %s units @ %s\n",
        $position->securityId->uniqueId,
        $position->units,
        $position->unitPrice
    );
}

// Investment transactions (buys, sells, dividends, etc.)
$txnList = $statement->investmentTransactionList;
foreach ($txnList->buyStock ?? [] as $buy) {
    echo "BUY: " . $buy->securityId->uniqueId . "\n";
}

$ofx = $parser->parseFile('statement.ofx');

// Header is available after parsing
$header = $parser->parsedHeader;

echo "OFX Version: " . $header->version . "\n";       // 102, 160, 200, 220, etc.
echo "Is v1 (SGML): " . ($header->isVersion1 ? 'Yes' : 'No') . "\n";
echo "Is v2 (XML): " . ($header->isVersion2 ? 'Yes' : 'No') . "\n";
echo "Encoding: " . $header->encoding . "\n";         // UTF-8, Windows-1252, etc.

// From file path
$ofx = $parser->parseFile('/path/to/file.ofx');

// From string content
$content = file_get_contents('statement.ofx');
$ofx = $parser->parseString($content);

// From stream resource
$stream = fopen('statement.ofx', 'r');
$ofx = $parser->parseStream($stream);
fclose($stream);

$ofx = $parser->parseFile('statement.ofx');

$signon = $ofx->signonMessagesResponseV1->signonResponse;

if ($signon->status->isSuccess()) {
    echo "Signed on successfully\n";
    echo "Server date: " . $signon->serverDate->format('Y-m-d H:i:s') . "\n";
    echo "Language: " . $signon->language->value . "\n";
    
    if ($signon->financialInstitution !== null) {
        echo "FI: " . $signon->financialInstitution->organization . "\n";
    }
} else {
    echo "Signon failed: " . $signon->status->code . "\n";
    echo "Message: " . $signon->status->message . "\n";
}