gitmathias/ofxparser

支持投资的 OFX 文件解析器

2.2.0 2024-03-21 16:28 UTC

This package is auto-updated.

Last update: 2024-09-21 17:51:10 UTC


README

Unit Tests

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

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

安装

简单地使用 Composer 引入该包

$ composer require gitmathias/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;

最常见的节点是 support。如果在您的 OFX 文件中遇到无法访问的节点,请提交一个 pull request!

投资支持

投资与银行/信用卡交易有很大不同。此版本根据作者立即的需求,支持 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) {
            // ...
        }

    }
}

分支 & 致谢

基于存档项目 asgrim/ofxparser 的分支。

存档最初是从 grimfor/ofxparser 分支出来的,并调整为框架无关。由 Oliver Lowe 重构,并松散地基于 Andrew A. Smith 的 ruby ofx-parser