$msgseqnum=1;
  
  function CreateFIXMSG($msg)
  {
    global $msgseqnum;
    
    # http://www.fixprotocol.org/FIXimate3.0/en/FIX.4.2/tag35.html
    # 52 SendingTime  
    # 35 is the MSG type
    
    $d=chr(1);
    $msg.="34=$msgseqnum\n52=".date("Ymd-H:i:s.u")."\n";
    $msg=str_replace("\n",$d,$msg);
    $msgseqnum++;

    # compute msglength (field 9)
    $msglen=strlen($msg);
    $msg="8=FIX.4.2{$d}9=$msglen{$d}".$msg;
        
    # compute checksum (field 10)
    $tot=0;
    for ($ii=0; $ii($begin+1)) { $timeout=true; }
    }
  }  

/* should return:
 
  8=FIX.4.2
  9=93
  35=A
  49=ACCEPTOR-PRICES-TEST
  56=INITIATOR-PRICES-TEST
  34=1
  52=20100322-16:36:04.734
  108=60
  141=Y
  98=0
  10=086
*/      

$shm_id = shmop_open(0x001, "c", 0644, 100);
$shm_size = shmop_size($shm_id);

while (true)
{

  # check shared memory 
  
  usleep(100);
  $arr=split("\|",shmop_read($shm_id, 0, 100));
  
  if ($arr[0]=="R") 
  { # we need to provide a quote to this location
    print "QUOTE:\n";
    $msg=CreateFIXMSG(
"35=R
49=INITIATOR-PRICES-TEST
56=ACCEPTOR-PRICES-TEST
15=USD
38=100
40=C
55=GBP/USD
64=SP
131=QID1
146=1
167=FOR
");

    if ($socket_pointer!==false)
    { # should be ok to write and read msg
      fwrite( $socket_pointer, "$msg" );
      $i=0;
      $begin=microtime(true);
      $timeout=false;
      $response="";
      while (!feof($socket_pointer) && ($timeout===false))
      {
        $c=fgetc($socket_pointer);
        $response.=$c;
        if ($c==chr(1)) 
        { # check if the last 7chars are 10=101|
          if (substr(substr($response,-7),0,3)=="10=")
          {
            print "$response\n";
            shmop_write($shm_id, $response, 0);
            $fp = fsockopen("localhost", 10002, $errno, $errstr, 30);
            if (!$fp)
            {
              echo "$errstr ($errno)
\n"; } else { fwrite($fp, ""); } fclose($fp); $timeout=true; } } $now=microtime(true); if (microtime(true)>($begin+1)) { $timeout=true; } } } } }