title: TCP sockets in PHP
body: <p>I am trying to connect to a ZKteco device using TCP sockets in php. There are a lot of libraries that uses php socket to connect to Zkteco devices but they are using UDP protocol. I want to connect using TCP protocol. There is <a href="https://github.com/adrobinoga/pyzatt" rel="nofollow noreferrer">this</a> library that uses TCP protocol but it is in Python. I want to covert it into Php.</p>
  
  <p>Now I am struggling in creating packets that will be sent to the Zkteco device.</p>
  
  <pre><code>self.soc_zk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  self.soc_zk.connect((ip_addr, dev_port))
  
  # send connect command
  #CMD_CONNECT = 0x03e8
  
  self.send_command(CMD_CONNECT)
  
  def send_command(self, cmd, data=None):
      self.soc_zk.send(self.create_packet(cmd, data))
  
  def create_packet(self, cmd_code, data=None, session_id=None,reply_number=None):
  
      zk_packet = bytearray(START_TAG)  # fixed tag
      zk_packet.extend([0x00] * 2)  # size of payload
      zk_packet.extend([0x00] * 2)  # fixed zeros
      zk_packet.extend(struct.pack('&lt;H', cmd_code))  # cmd code / reply id
      zk_packet.extend([0x00] * 2)  # checksum field
  
      # append session id
      if session_id is None:
          zk_packet.extend(struct.pack('&lt;H', self.session_id))
      else:
          zk_packet.extend(struct.pack('&lt;H', session_id))
  
      # append reply number
      if reply_number is None:
          zk_packet.extend(struct.pack('&lt;H', self.reply_number))
      else:
          zk_packet.extend(struct.pack('&lt;H', reply_number))
  
      # append additional data
      if data:
          zk_packet.extend(data)
  
      # write size field
      zk_packet[4:6] = struct.pack('&lt;H', len(zk_packet) - 8)
      # write checksum
      zk_packet[10:12] = struct.pack('&lt;H', checksum16(zk_packet[8:]))
  
      return zk_packet
  </code></pre>