1

Topic: Error 400--Bad Request

Jesse,

I'm getting a bad request error when I try posting the following OFX request:

OFXHEADER:100 DATA:OFXSGML VERSION:102 SECURITY:NONE ENCODING:USASCII CHARSET:1252 COMPRESSION:NONE OLDFILEUID:NONE NEWFILEUID:481AB8B4635D9 <OFX><SIGNONMSGSRQV1><SONRQ><DTCLIENT>20080502014612<USERID>user<USERPASS>password<LANGUAGE>ENG<FI><ORG>Ameritrade</FI> <APPID>QWIN<APPVER>1200</SONRQ> </SIGNONMSGSRQV1> <INVSTMTMSGSRQV1><INVSTMTTRNRQ><TRNUID>481AB8B46374C<CLTCOOKIE>4<INVSTMTRQ><INVACCTFROM><BROKERID>ameritrade<ACCTID>accountNumber</INVACCTFROM> <INCTRAN><DTSTART>20060101000000<INCLUDE>Y</INCTRAN> <INCOO>Y<INCPOS><DTASOF>20080502014612<INCLUDE>Y</INCPOS> <INCBAL>Y</INVSTMTRQ> </INVSTMTTRNRQ> </INVSTMTMSGSRQV1> </OFX>

Any ideas?

Thanks

Re: Error 400--Bad Request

Hi pgp78,

Here is an ameritrade OFX request that I generated from php:

OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:7A0CDEBA-DE2B-493F-BABB-F507FDAC3027

<OFX>
<SIGNONMSGSRQV1>
<SONRQ>
<DTCLIENT>20080502083809
<USERID>username
<USERPASS>password
<LANGUAGE>ENG
<FI>
<ORG>ameritrade.com
</FI>
<APPID>Money
<APPVER>1600
</SONRQ>
</SIGNONMSGSRQV1>
<INVSTMTMSGSRQV1>
<INVSTMTTRNRQ>
<TRNUID>8CCCCD65-13AF-4464-8990-5A0E108ACA3E
<CLTCOOKIE>4
<INVSTMTRQ>
<INVACCTFROM>
<BROKERID>ameritrade.com
<ACCTID>account
</INVACCTFROM>
<INCTRAN>
<DTSTART>19000101
<INCLUDE>Y
</INCTRAN>
<INCOO>Y
<INCPOS>
<DTASOF>20080502083809
<INCLUDE>Y
</INCPOS>
<INCBAL>Y
</INVSTMTRQ>
</INVSTMTTRNRQ>
</INVSTMTMSGSRQV1>
</OFX>

The differences I notice are that org and brokerid are different.  However I just downloaded an OFX file from ameritrade using the ofxget program which uses ofxhome values (the ones you are using).  The app type is different, too.  Other than that the differences seem minor.

When you do a phpinfo() what information is given from curl?  Here is my output:

curl

cURL support => enabled
cURL Information => libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3

I wonder if it's possible that curl does not have SSL support?  If you are getting a 400 error code then the problem is possibly with the network communication, not OFX.

-- jesse

Re: Error 400--Bad Request

pgp78 wrote:

After some changes I made to the OFX request, I got it to work.  Looks like I had an extra carriage return in there somewhere.

A few questions.

1.  When you build your OFX request in PHP, do you use < and &gt for the < and > symbols.  Or, is this not needed since you're sending the request directly to an OFX server and not displaying them in HTML?  I assume &lt and $gt is not needed.

2.  Do you have to do something special to have the response be in OFX format?  When I use PHP echo() to display the reaponse, there is a lot of transaction data but none of it is in the OFX field format.  Here's an example:

SELL TRADE 345203202 CUSIP -1399 2.5 9.99 0.04 3487.47 MARGIN MARGIN SELL 3696930710 20080229235538 MARGIN INTEREST ADJUSTMENT -20.83 MARGIN 3623929901 20080131232115 MARGIN INTEREST ADJUSTMENT -28.32 MARGIN 3513772284 20071231032713 MARGIN INTEREST ADJUSTMENT -57.92 MARGIN 3444993712 20071219151356 SELL TRADE 345203202 CUSIP -500 4.75 9.99 0.04 2364.97 MARGIN MARGIN SELL 3375345280 20071130231043 MARGIN INTEREST ADJUSTMENT -90.21 MARGIN 3268866029

Do I need to run the OFX output through a PHP function to make it HTML friendly?

3.  I've seen some people parse OFX by first converting OFX's SGML into XML first.  Do you have any suggestions if this is a good approach or if there are easier ways to parse OFX?

Thank you for your help.

I'm glad you got it working.  OFX servers are quite finicky in my experience.

1. SGML angled brackets such as <OFX> and <SIGNONMSGSRQV1> should not be converted to < and >.  If angled brackets occur somewhere else (say as part of the brokerid) then I am not sure what to do.  I don't even know if this is legal.  If you find out let me know.

2. The browser is probably getting confused about the angled brackets and treating them as invalid HTML markup.  You can check that the output is proper by viewing the HTML source.  That will show it as regular text.  You can do a str_replace to change all < and > to < and >.  I am sure there are other functions that will do the conversion.

3. I think this is the easiest way to go.  There is a program called osx that is part of the opensp library.  It converts SGML to XML.  It works well.  You can launch osx from php using a system() call.  If you find a better alternative that runs natively in php I'd like to know about it.  That would make my life a lot easier.

-- jesse

Re: Error 400--Bad Request

Hey guys,

I know this is an old post, but just thought I'd add some for future readers:

Much like the SOAP protocol, which takes responses in XML format, you can parse the raw response with PHP using the simpleXMLElement() class:

http://php.net/manual/en/class.simplexmlelement.php