franciscoalex2407 / ofxparser
简单的 OFX 文件解析器
dev-master
2023-05-08 14:55 UTC
Requires
- php: ~5.6|~7.0
Requires (Dev)
- phpunit/phpunit: ~5.5
- squizlabs/php_codesniffer: ~2.6
This package is auto-updated.
Last update: 2024-09-08 18:06:55 UTC
README
OFX 解析器是一个 PHP 库,用于将金融机构下载的 OFX 文件解析成简单的 PHP 对象。
它支持多个银行账户、所需的“登录”响应,并识别 OFX 时间戳。
安装
只需使用 Composer 引入此包
$ composer require franciscoalex2407/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 文件中遇到无法访问的节点,请提交一个 pull 请求!
投资支持
投资看起来与银行/信用卡交易有很大不同。本版本根据作者立即的需求,支持 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。