关于php-mqtt的坑整理笔记

1.stream_socket_client函数。这个函数在php5.6版本以前,tcp/tls链接报错很模糊。比如链接mqtt的时候,服务器需要证书,不传递不传递证书,证书对不对都会报一个类似错误 Uninitialized string offset。意思就是phpMQQT.php

if(ord($string{0})>>4 == 2 && $string{3} == chr(0)){
            if($this->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;
        }

这里string索引越界。

解决方法,升级php版本到5.6+

2.关于证书问题如果服务器需要证书,这个证书需要同一个openssl签发。如果服务器有证书,请把证书拷贝到需要连接mqtt服务器上。并且证书路径一定要写服务器绝对路径。比如/etc/cert/ca.crt
phpMQQT.php中,默认的publish.php和subscribe.php是没有写证书。
证书可以在初始化的时候传入。

$mqtt = new phpMQTT($server, $port, $client_id,$cert);

3.phpMQTT下载路径
https://github.com/bluerhinos/phpMQTT

你可能感兴趣的:(关于php-mqtt的坑整理笔记)