尝试使用PHP cURL发送OFX HTTP POST时出错

时间:2014-02-23 18:20:07

标签: php curl ofx

我计划建立一个自己的财务管理应用程序供个人使用。我通过Chase启用了直接连接服务,但是我遇到了一些从服务器获得响应的问题。这是我正在使用的代码。有人很好,可以在另一个论坛上发布,供其他人使用。

<?php

// this function is just used to generate a random transaction id below
function getRandomString($length = 40, $charset = 'alphanum') {
$alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$num = '0123456789';
switch ($charset) {
    case 'alpha':
        $chars = $alpha;
        break;
    case 'alphanum':
        $chars = $alpha . $num;
        break;
    case 'num':
        $chars = $num;
        break;            
}

$randstring='';
$maxvalue=strlen($chars)-1;
for ($i = 0; $i < $length; $i++)
  $randstring .= substr($chars, rand(0, $maxvalue), 1);
return $randstring;
}

// user login info
$user = 'username';
$pass = 'password';

// account info
$accnt_num = '00000000';
$accnt_type = 'CHECKING';

// date and a random unique transaction id
$txn_id = getRandomString(6);
$tz_offset = date('Z')/3600;
$date = date("YmdHis[$tz_offset:T]");

// Chase info
$org = 'B1';
$fid = '10898';
$bank_id = '122100024';

// transaction retrieval date range
$start_date = '20090101000000';        // from date, to..
$end_date = date('YmdHis');        // now

$xml = "
<OFX>
    <SIGNONMSGSRQV1>
    <SONRQ>
        <DTCLIENT>$date</DTCLIENT>
        <USERID>$user</USERID>
        <USERPASS>$pass</USERPASS>
        <LANGUAGE>ENG</LANGUAGE>
        <FI>
            <ORG>$org</ORG>
            <FID>$fid</FID>
        </FI>
        <APPID>QWIN</APPID>
        <APPVER>2200</APPVER>
    </SONRQ>
</SIGNONMSGSRQV1>

<BANKMSGSRQV1>
    <STMTTRNRQ>
        <TRNUID>$txn_id</TRNUID>
        <STMTRQ>
            <BANKACCTFROM>
                <BANKID>$bank_id</BANKID>
                <ACCTID>$accnt_num</ACCTID>
                <ACCTTYPE>$accnt_type</ACCTTYPE>
            </BANKACCTFROM>
            <INCTRAN>
                <DTSTART>$start_date</DTSTART>
                <DTEND>$end_date</DTEND>
                <INCLUDE>Y</INCLUDE>
            </INCTRAN>
        </STMTRQ>
    </STMTTRNRQ>
</BANKMSGSRQV1>
</OFX>";

$ch = curl_init();
$ch1 = curl_setopt($ch, CURLOPT_URL, 'https://ofx.chase.com');
$ch2 = curl_setopt($ch, CURLOPT_POST, 1);
$ch3 = curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
$ch4 = curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$ch5 = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch6 = curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if ($result) {
    echo $result;
}
    else
echo "<pre>CURL ERROR: " . curl_error($ch) . "</pre>";
curl_close ($ch);
?>

尝试从浏览器运行时收到以下消息。

CURL ERROR: SSL read: error:00000000:lib(0):func(0):reason(0), errno 0

我在另一篇文章中看到你可能需要更新cacert.pem证书,所以我下载了新版本并将其添加到我的php.ini文件中,但它没有帮助。

curl.cainfo = "C:\xampp\PHP\cacert.pem"

如果我使用curl_errno而不是curl_error,我得到卷曲错误代码56,即CURLE_RECV_ERROR(56)接收网络数据失败。

我知道追逐服务器可用于OFX下载,因为我可以通过GNUCash财务经理获取我的银行信息。任何人可以提供的帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我最近一直在做类似的事情,而且由于我真的无法解释的原因,我无法获得你用来工作的相同代码片段。但我确实对this php library有好运。你会得到一个漂亮而又整洁的$ finance对象。我攻击了帐户类以获得$transactions属性,我后来填写了这些属性,因为该信息可以从已经发出的OFX请求获得。

我将此添加到Account-&gt; setup()

的末尾
    ...
    $a = $x->xpath('/OFX/*/*/*/AVAILBAL/BALAMT');
    if(isset($a[0]))
        $this->availableBalance = (double)$a[0];

// added code below
    if($this->type == 'BANK')
        $this->transactions = $x->xpath('/OFX/BANKMSGSRSV1/STMTTRNRS/STMTRS/BANKTRANLIST/STMTTRN');
    elseif($this->type == 'CC')
        $this->transactions = $x->xpath('/OFX/CREDITCARDMSGSRSV1/CCSTMTTRNRS/CCSTMTRS/BANKTRANLIST/STMTTRN');

希望这有帮助!