title: phpMQTT publish not working
body: <p>I have installed <a href="http://mosquitto.org" rel="nofollow">Mosquitto</a> on the server, and it's running, and working fine.
  I have installed Paho mqtt client plugin to check.
  and I wrote the code for android and it's working fine on the emulator.</p>
  
  <p>now in my own computer everything works fine, I run the php code and my app in android receives the message and Paho client receives that message too.</p>
  
  <p>but when I upload the file on the server and run it nothing happens, although the php code runs everything successfully, and publishes the message without any error.
  and even the mosquitto's publisher executable works fine and clients receive the message.</p>
  
  <p>only this php code on the server doesn't do anything. </p>
  
  <pre><code>$mqtt = new phpMQTT("localhost", 1883, "server"); 
  
          if ($mqtt-&gt;connect()) {
              echo"connected\n";
              for($i=0;$i&lt;4;++$i){
  
                  if($mqtt-&gt;publish("new","Hey . ".$i,2)){
                      echo "published\n";
                  }
                  else{
                      echo "not published\n";
                  }
              }
              $mqtt-&gt;close();
          }
          else{
              echo"couldn't connect\n";
          }
  </code></pre>
  
  <p><a href="https://github.com/bluerhinos/phpMQTT" rel="nofollow">Bluerhinos phpMQTT</a>:</p>
  
  <pre><code>&lt;?php
  /* phpMQTT */
  class phpMQTT {
  
      private $socket;            /* holds the socket */
      private $msgid = 1;         /* counter for message id */
      public $keepalive = 10;     /* default keepalive timmer */
      public $timesinceping;      /* host unix time, used to detect disconects */
      public $topics = array();   /* used to store currently subscribed topics */
      public $debug = true;       /* should output debug messages */
      public $address;            /* broker address */
      public $port;               /* broker port */
      public $clientid;           /* client id sent to brocker */
      public $will;               /* stores the will of the client */
      private $username;          /* stores username */
      private $password;          /* stores password */
  
      function __construct($address, $port, $clientid){
          $this-&gt;broker($address, $port, $clientid);
      }
  
      /* sets the broker details */
      function broker($address, $port, $clientid){
          $this-&gt;address = $address;
          $this-&gt;port = $port;
          $this-&gt;clientid = $clientid;        
      }
  
      /* connects to the broker 
          inputs: $clean: should the client send a clean session flag */
      function connect($clean = true, $will = NULL, $username = NULL, $password = NULL){
  
          if($will) $this-&gt;will = $will;
          if($username) $this-&gt;username = $username;
          if($password) $this-&gt;password = $password;
  
          $address = $this-&gt;address;  
          $this-&gt;socket = fsockopen($address, $this-&gt;port, $errno, $errstr, 60);
  
          if (!$this-&gt;socket ) {
              error_log("fsockopen() $errno, $errstr \n");
              return false;
          }
  
          stream_set_timeout($this-&gt;socket, 5);
          stream_set_blocking($this-&gt;socket, 0);
  
          $i = 0;
          $buffer = "";
  
          $buffer .= chr(0x00); $i++;
          $buffer .= chr(0x06); $i++;
          $buffer .= chr(0x4d); $i++;
          $buffer .= chr(0x51); $i++;
          $buffer .= chr(0x49); $i++;
          $buffer .= chr(0x73); $i++;
          $buffer .= chr(0x64); $i++;
          $buffer .= chr(0x70); $i++;
          $buffer .= chr(0x03); $i++;
  
          //No Will
          $var = 0;
          if($clean) $var+=2;
  
          //Add will info to header
          if($this-&gt;will != NULL){
              $var += 4; // Set will flag
              $var += ($this-&gt;will['qos'] &lt;&lt; 3); //Set will qos
              if($this-&gt;will['retain'])   $var += 32; //Set will retain
          }
  
          if($this-&gt;username != NULL) $var += 128;    //Add username to header
          if($this-&gt;password != NULL) $var += 64; //Add password to header
  
          $buffer .= chr($var); $i++;
  
          //Keep alive
          $buffer .= chr($this-&gt;keepalive &gt;&gt; 8); $i++;
          $buffer .= chr($this-&gt;keepalive &amp; 0xff); $i++;
  
          $buffer .= $this-&gt;strwritestring($this-&gt;clientid,$i);
  
          //Adding will to payload
          if($this-&gt;will != NULL){
              $buffer .= $this-&gt;strwritestring($this-&gt;will['topic'],$i);  
              $buffer .= $this-&gt;strwritestring($this-&gt;will['content'],$i);
          }
  
          if($this-&gt;username) $buffer .= $this-&gt;strwritestring($this-&gt;username,$i);
          if($this-&gt;password) $buffer .= $this-&gt;strwritestring($this-&gt;password,$i);
  
          $head = "  ";
          $head{0} = chr(0x10);
          $head{1} = chr($i);
  
          fwrite($this-&gt;socket, $head, 2);
          fwrite($this-&gt;socket,  $buffer);
  
          $string = $this-&gt;read(4);
  
          if(ord($string{0})&gt;&gt;4 == 2 &amp;&amp; $string{3} == chr(0)){
              if($this-&gt;debug) echo "Connected to Broker\n"; 
          }else{  
              error_log(sprintf("Connection failed! (Error: 0x%02x 0x%02x)\n", 
                                      ord($string{0}),ord($string{3})));
              return false;
          }
  
          $this-&gt;timesinceping = time();
  
          return true;
      }
  
      /* read: reads in so many bytes */
      function read($int = 8192, $nb = false){
  
          //  print_r(socket_get_status($this-&gt;socket));
  
          $string="";
          $togo = $int;
  
          if($nb){
              return fread($this-&gt;socket, $togo);
          }
  
          while (!feof($this-&gt;socket) &amp;&amp; $togo&gt;0) {
              $fread = fread($this-&gt;socket, $togo);
              $string .= $fread;
              $togo = $int - strlen($string);
          }
  
  
  
  
              return $string;
      }
  
      /* subscribe: subscribes to topics */
      function subscribe($topics, $qos = 0){
          $i = 0;
          $buffer = "";
          $id = $this-&gt;msgid;
          $buffer .= chr($id &gt;&gt; 8);  $i++;
          $buffer .= chr($id % 256);  $i++;
  
          foreach($topics as $key =&gt; $topic){
              $buffer .= $this-&gt;strwritestring($key,$i);
              $buffer .= chr($topic["qos"]);  $i++;
              $this-&gt;topics[$key] = $topic; 
          }
  
          $cmd = 0x80;
          //$qos
          $cmd += ($qos &lt;&lt; 1);
  
  
          $head = chr($cmd);
          $head .= chr($i);
  
          fwrite($this-&gt;socket, $head, 2);
          fwrite($this-&gt;socket, $buffer, $i);
          $string = $this-&gt;read(2);
  
          $bytes = ord(substr($string,1,1));
          $string = $this-&gt;read($bytes);
      }
  
      /* ping: sends a keep alive ping */
      function ping(){
              $head = " ";
              $head = chr(0xc0);      
              $head .= chr(0x00);
              fwrite($this-&gt;socket, $head, 2);
              if($this-&gt;debug) echo "ping sent\n";
      }
  
      /* disconnect: sends a proper disconect cmd */
      function disconnect(){
              $head = " ";
              $head{0} = chr(0xe0);       
              $head{1} = chr(0x00);
              fwrite($this-&gt;socket, $head, 2);
      }
  
      /* close: sends a proper disconect, then closes the socket */
      function close(){
          $this-&gt;disconnect();
          fclose($this-&gt;socket);  
      }
  
      /* publish: publishes $content on a $topic */
      function publish($topic, $content, $qos = 0, $retain = 0){
  
          $i = 0;
          $buffer = "";
  
          $buffer .= $this-&gt;strwritestring($topic,$i);
  
          //$buffer .= $this-&gt;strwritestring($content,$i);
  
          if($qos){
              $id = $this-&gt;msgid++;
              $buffer .= chr($id &gt;&gt; 8);  $i++;
              $buffer .= chr($id % 256);  $i++;
          }
  
          $buffer .= $content;
          $i+=strlen($content);
  
  
          $head = " ";
          $cmd = 0x30;
          if($qos) $cmd += $qos &lt;&lt; 1;
          if($retain) $cmd += 1;
  
          $head{0} = chr($cmd);       
          $head .= $this-&gt;setmsglength($i);
  
          if((fwrite($this-&gt;socket, $head, strlen($head)))&amp;&amp;(fwrite($this-&gt;socket, $buffer, $i))){
              echo "done: ".$this-&gt;socket."&lt;br&gt;";
              return true;
          }
          else {
              echo $head;
              return false;
          }
  
      }
  
      /* message: processes a recieved topic */
      function message($msg){
              $tlen = (ord($msg{0})&lt;&lt;8) + ord($msg{1});
              $topic = substr($msg,2,$tlen);
              $msg = substr($msg,($tlen+2));
              $found = 0;
              foreach($this-&gt;topics as $key=&gt;$top){
                  if( preg_match("/^".str_replace("#",".*",
                          str_replace("+","[^\/]*",
                              str_replace("/","\/",
                                  str_replace("$",'\$',
                                      $key))))."$/",$topic) ){
                      if(function_exists($top['function'])){
                          call_user_func($top['function'],$topic,$msg);
                          $found = 1;
                      }
                  }
              }
  
              if($this-&gt;debug &amp;&amp; !$found) echo "msg recieved but no match in subscriptions\n";
      }
  
      /* proc: the processing loop for an "allways on" client 
          set true when you are doing other stuff in the loop good for watching something else at the same time */    
      function proc( $loop = true){
  
          if(1){
              $sockets = array($this-&gt;socket);
              $w = $e = NULL;
              $cmd = 0;
  
                  //$byte = fgetc($this-&gt;socket);
              if(feof($this-&gt;socket)){
                  if($this-&gt;debug) echo "eof receive going to reconnect for good measure\n";
                  fclose($this-&gt;socket);
                  $this-&gt;connect(false);
                  if(count($this-&gt;topics))
                      $this-&gt;subscribe($this-&gt;topics);    
              }
  
              $byte = $this-&gt;read(1, true);
  
              if(!strlen($byte)){
                  if($loop){
                      usleep(100000);
                  }
  
              }else{ 
  
                  $cmd = (int)(ord($byte)/16);
                  if($this-&gt;debug) echo "Recevid: $cmd\n";
  
                  $multiplier = 1; 
                  $value = 0;
                  do{
                      $digit = ord($this-&gt;read(1));
                      $value += ($digit &amp; 127) * $multiplier; 
                      $multiplier *= 128;
                      }while (($digit &amp; 128) != 0);
  
                  if($this-&gt;debug) echo "Fetching: $value\n";
  
                  if($value)
                      $string = $this-&gt;read($value,"fetch");
  
                  if($cmd){
                      switch($cmd){
                          case 3:
                              $this-&gt;message($string);
                          break;
                      }
  
                      $this-&gt;timesinceping = time();
                  }
              }
  
              if($this-&gt;timesinceping &lt; (time() - $this-&gt;keepalive )){
                  if($this-&gt;debug) echo "not found something so ping\n";
                  $this-&gt;ping();  
              }
  
  
              if($this-&gt;timesinceping&lt;(time()-($this-&gt;keepalive*2))){
                  if($this-&gt;debug) echo "not seen a package in a while, disconnecting\n";
                  fclose($this-&gt;socket);
                  $this-&gt;connect(false);
                  if(count($this-&gt;topics))
                      $this-&gt;subscribe($this-&gt;topics);
              }
  
          }
          return 1;
      }
  
      /* getmsglength: */
      function getmsglength(&amp;$msg, &amp;$i){
  
          $multiplier = 1; 
          $value = 0 ;
          do{
            $digit = ord($msg{$i});
            $value += ($digit &amp; 127) * $multiplier; 
            $multiplier *= 128;
            $i++;
          }while (($digit &amp; 128) != 0);
  
          return $value;
      }
  
  
      /* setmsglength: */
      function setmsglength($len){
          $string = "";
          do{
            $digit = $len % 128;
            $len = $len &gt;&gt; 7;
            // if there are more digits to encode, set the top bit of this digit
            if ( $len &gt; 0 )
              $digit = ($digit | 0x80);
            $string .= chr($digit);
          }while ( $len &gt; 0 );
          return $string;
      }
  
      /* strwritestring: writes a string to a buffer */
      function strwritestring($str, &amp;$i){
          $ret = " ";
          $len = strlen($str);
          $msb = $len &gt;&gt; 8;
          $lsb = $len % 256;
          $ret = chr($msb);
          $ret .= chr($lsb);
          $ret .= $str;
          $i += ($len+2);
          return $ret;
      }
  
      function printstr($string){
          $strlen = strlen($string);
              for($j=0;$j&lt;$strlen;$j++){
                  $num = ord($string{$j});
                  if($num &gt; 31) 
                      $chr = $string{$j}; else $chr = " ";
                  printf("%4d: %08b : 0x%02x : %s \n",$j,$num,$num,$chr);
              }
      }
  }
  
  ?&gt;
  </code></pre>