保罗/ofxparser

简单的 OFX 文件解析器

1.2.2 2022-11-18 17:23 UTC

This package is auto-updated.

Last update: 2024-09-18 22:11:56 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version License

OFX 解析器是一个 PHP 库,旨在将金融机构下载的 OFX 文件解析为简单的 PHP 对象。

它支持多个银行账户、所需的“登录”响应,并识别 OFX 时间戳。

安装

只需使用 Composer 引入该包

$ composer require asgrim/ofxparser

使用方法

您可以按以下方式访问 OFX 文件中的节点

$ofxParser = new \OfxParser\Parser();
$ofx = $ofxParser->loadFromFile('/path/to/your/bankstatement.ofx');

$bankAccount = reset($ofx->bankAccounts);

// Get the statement start and end dates
$startDate = $bankAccount->statement->startDate;
$endDate = $bankAccount->statement->endDate;

// Get the statement transactions for the account
$transactions = $bankAccount->statement->transactions;

最常见的节点是支持。如果您在 OFX 文件中发现无法访问的节点,请提交一个拉取请求!

投资支持

与银行/信用卡交易相比,投资看起来有很大不同。本版本支持 OFX 2.0.3 规范中的一部分节点,以满足作者们的直接需求。如果您选择实现此库,请参考 OFX 文档。特别是,它目前不处理投资头寸(INVPOSLIST)或引用的安全定义(SECINFO)。

这不是字面意义上的字段透传,例如 python 中的此实现:csingley/ofxtools。此包包含一些偶尔“翻译”的字段,使其对不太熟悉投资 OFX 规范的用户更友好。

从 Quicken(QFX)文件或 MS Money(OFX/XML)文件中加载投资

// You'll probably want to alias the namespace:
use OfxParser\Entities\Investment as InvEntities;

// Load the OFX file
$ofxParser = new \OfxParser\Parsers\Investment();
$ofx = $ofxParser->loadFromFile('/path/to/your/investments_file.ofx');

// Loop over investment accounts (named bankAccounts from base lib)
foreach ($ofx->bankAccounts as $accountData) {
    // Loop over transactions
    foreach ($accountData->statement->transactions as $ofxEntity) {
        // Keep in mind... not all properties are inherited for all transaction types...

        // Maybe you'll want to do something based on the transaction properties:
        $nodeName = $ofxEntity->nodeName;
        if ($nodeName == 'BUYSTOCK') {
            // @see OfxParser\Entities\Investment\Transaction...

            $amount = abs($ofxEntity->total);
            $cusip = $ofxEntity->securityId;

            // ...
        }

        // Maybe you'll want to do something based on the entity:
        if ($ofxEntity instanceof InvEntities\Transaction\BuyStock) {
            // ...
        }

    }
}

分支 & 信用

这是从 grimfor/ofxparser 分支出来,为了使其不依赖于框架而制作的。源代码库是为 Symfony 2 框架设计的,因此应该给予应有的信用!此代码库主要由 Oliver Lowe 重构,并大致基于 Andrew A. Smith 的 Ruby ofx-parser