3

I have pretty basic knowledge of PHP sockets and the FIX protocol altogether. I have an account that allows me to connect to a server and retrieve currency prices.

I adapted this code to connect and figure out what I receive back from the remote server:

$host = "the-server.com";
    $port = "2xxxx";

    $fixv = "8=FIX.4.2";
    $clid = "client-name";
    $tid = "target-name";

    $fp = fsockopen($host, $port, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out = "$fixv|9=70|35=A|49=$clid|56=$tid|34=1|52=20000426-12:05:06|98=0|108=30|10=185|";
        echo "\n".$out."\n";
        fwrite($fp, $out);
        while (!feof($fp)) {
            echo ".";
            echo fgets($fp, 1024);
        }
        fclose($fp);
    }

and I get nothing back. The host is good because I'm getting an error when I use a random one. Is the message I'm sending not generating a reply ?

I might not be very good at finding things in Google but I could not find any simple tutorial on how to do this with php (at least nothing that puts together fix and php).

Any help is greatly appreciated.

NickOpris
  • 509
  • 2
  • 8
  • 20
  • More info on this http://javarevisited.blogspot.com/2011/02/fix-protocol-session-or-admin-messages.html I believe I followed the logon instructions properly so I guess either my code doesn't read the reply properly or is there a different issue... – NickOpris Aug 09 '11 at 21:00
  • Hi NickO, did you get this to work in the end? – Christian Jun 03 '13 at 14:22
  • I think that project requirements changed at that time and we found a different way to do it. – NickOpris Nov 03 '13 at 11:10
  • Actually is PHP capable to communicate with FIX server ? – neobie Aug 27 '16 at 04:06

2 Answers2

4

FIX separator character is actually '\001' not '|', so you have to replace that when sending.

Some links for you:

Edit 0:

From that same wikipedia article you mention:

The message fields are delimited using the ASCII 01 character.
...
Example of a FIX message : Execution Report (Pipe character is used to represent SOH character) ...

Edit 1:

Couple more points:

  • Tag 9 holds message length without tags 8 (type), 9 (length), and 10 (checksum).
  • Tag 10, checksum, has to be a modulo 256 sum of ASCII values of all message characters including all SOH separators, but not including the tag 10 itself (I know, it's stupid to have checksums on top of TCP, but ...)
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • The message I built was checked with http://www.validfix.com/fix-analyzer.html 8=FIX.4.2|9=53|35=A|49=a|56=b|34=1|52=20000426-12:05:06|98=0|108=30|10=237| – NickOpris Aug 09 '11 at 21:08
  • also here is an example of a FIX message using the | separator http://en.wikipedia.org/wiki/FIX_protocol – NickOpris Aug 09 '11 at 21:17
  • 2
    I am talking about the wire format. You might also want to include `fflush()` after the write. – Nikolai Fetissov Aug 09 '11 at 21:19
  • Just noticed that after you mentioned it. I'm reformatting the message but my question is still - even if that message is wrongly formatted, should I not get something, anything back? – NickOpris Aug 09 '11 at 21:36
  • 1
    That's totally server implementation dependent. I saw places that just drop your connection on malformed message. Also, don't forget to flush the output. – Nikolai Fetissov Aug 09 '11 at 21:40
  • I must miss something because I am unable to get any sort of message back from the server. I have my simple code here - http://pastebin.com/9sxYXgyd . Any ideas? – NickOpris Aug 16 '11 at 21:52
  • line 26 in the above code has been updated to read 108 instead of 08 – NickOpris Aug 16 '11 at 22:44
  • The only thing wrong I see is that you are missing `SOH` after the checksum, i.e. append `'\001'` to the very end of the message. Other then that - try sending some pre-canned message to the server with, say, `nc`. Do you get anything back? Pull out `wireshark` and check what's going on on the wire. – Nikolai Fetissov Aug 17 '11 at 03:14
0

The issue is the use of fgets(...), it is expecting a \n which does not exists in this FIX protocol.

On top of that, an expected length of 1024 is specified, which is a length that the response is unlikely to exceed.

To cap it off, since the server doesn't terminate the connection, fgets(...) hangs there "forever"

bilogic
  • 449
  • 3
  • 18