PHP标准MQTT使用composer autoload加载php-mqtt/client
<h2>PHP MQTT 使用指南</h2>
<h4>一、导入 composer autoload 文件和 php-mqtt/client</h4>
<pre><code class="language-php">require('vendor/autoload.php');
use \PhpMqtt\Client\MqttClient;</code></pre>
<h4>二、设置 MQTT Broker 连接参数1</h4>
<p>设置 MQTT Broker 连接地址,端口,客户端 ID,用户名以及 topic,这里我们调用 PHP rand 函数随机生成 MQTT 客户端 ID,避免与其他客户端 ID 重复。</p>
<pre><code class="language-php">$server = '39.98.75.131';
$port = 1883;
$clientId = rand(5, 15);
$username = 'mqttpay';
$password = mqttpay;
$clean_session = false;</code></pre>
<p>使用上述的参数进行连接,通过 ConnectionSettings 设置连接参数:</p>
<pre><code class="language-php">$connectionSettings = new ConnectionSettings();
$connectionSettings
-&gt;setUsername($username)
-&gt;setPassword($password)
-&gt;setKeepAliveInterval(60)
// Last Will 设置
-&gt;setLastWillTopic(设备IMEI)
-&gt;setLastWillMessage('client disconnect')
-&gt;setLastWillQualityOfService(1);</code></pre>
<h5>三、订阅消息</h5>
<p>编写代码订阅 emqx/test 主题,并为该订阅配置回调函数以处理接收到的消息,此处我们将订阅得到的主题和消息打印出来:</p>
<pre><code class="language-php">// 订阅
$mqtt-&gt;subscribe('emqx/test', function ($topic, $message) {
printf(&quot;Received message on topic [%s]: %s\n&quot;, $topic, $message);
}, 0);
</code></pre>
<h4>四、发布消息</h4>
<p>构造一个 payload,调用 publish 函数向 emqx/test 主题发布消息,发布完成之后客户端需要进入轮询状态,处理传入的消息和重发队列:</p>
<pre><code class="language-php">for ($i = 0; $i&lt; 10; $i++) {
$payload = array(
'protocol' =&gt; 'tcp',
'date' =&gt; date('Y-m-d H:i:s'),
'url' =&gt; 'https://github.com/emqx/MQTT-Client-Examples'
);
$mqtt-&gt;publish(
// topic
'emqx/test',
// payload
json_encode($payload),
// qos
0,
// retain
true
);
printf(&quot;msg $i send\n&quot;);
sleep(1);
}
// 客户端轮询以处理传入消息和重发队列
$mqtt-&gt;loop(true);
// 断开 MQTT 连接
// $client-&gt;disconnect();
</code></pre>
<h4>完整代码</h4>
<p>服务器连接、消息发布与接收代码:</p>
<pre><code class="language-php">&lt;?php
require('vendor/autoload.php');
use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;
$server = '39.98.75.131';
$port = 1883;
$clientId = rand(5, 15);
$username = 'mqttpay';
$password = 'mqttpay';
$clean_session = false;
$connectionSettings = new ConnectionSettings();
$connectionSettings
-&gt;setUsername($username)
-&gt;setPassword(null)
-&gt;setKeepAliveInterval(60)
// Last Will 设置
-&gt;setLastWillTopic('emqx/test/last-will')
-&gt;setLastWillMessage('client disconnect')
-&gt;setLastWillQualityOfService(1);
$mqtt = new MqttClient($server, $port, $clientId);
$mqtt-&gt;connect($connectionSettings, $clean_session);
printf(&quot;client connected\n&quot;);
$mqtt-&gt;subscribe('emqx/test', function ($topic, $message) {
printf(&quot;Received message on topic [%s]: %s\n&quot;, $topic, $message);
}, 0);
for ($i = 0; $i&lt; 10; $i++) {
$payload = array(
'protocol' =&gt; 'tcp',
'date' =&gt; date('Y-m-d H:i:s'),
'url' =&gt; 'https://github.com/emqx/MQTT-Client-Examples'
);
$mqtt-&gt;publish(
// topic
'emqx/test',
// payload
json_encode($payload),
// qos
0,
// retain
true
);
printf(&quot;msg $i send\n&quot;);
sleep(1);
}
$mqtt-&gt;loop(true);
// 断开 MQTT 连接
// $client-&gt;disconnect();
</code></pre>
<pre><code class="language-php">&lt;?php
use Mosquitto\Client;
## 接入点地址,购买实例后从控制台获取
$endPoint = '39.98.75.131';
$port = 1883;
$username = 'mqttpay';
$password = 'mqttpay';
## MQTT 客户端ID 后缀,DeviceId,业务方自由指定,需要保证全局唯一,禁止 2 个客户端连接使用同一个 ID
## 设备IMEI号码
$deviceId = '352736082691067';
$qos = 2;
$keepalive = 90;
$cleanSession = true;
$clientId = $deviceId;
## 发送端ClientID,请修改或添加特定字符串,保证全局唯一
$masterClientId='ZKC20191010master';
echo $clientId . &quot;\n&quot;;
$mid = 0;
$mqttClient = new Mosquitto\Client($masterClientId, $cleanSession);
echo &quot;UserName:&quot; . $username . &quot; Password:&quot; . $password . &quot;\n&quot;;
$mqttClient-&amp;setCredentials($username, $password);
## 设置连接成功回调
$mqttClient-&amp;onConnect(function ($rc, $message) use ($mqttClient, &amp;amp;$mid, $topic, $qos, $clientId) {
echo date('Y-m-d H:i:s').&quot; 连接成功:Connnect to Server Code is &quot; . $rc . &quot; message is &quot; . $message . &quot;\n&quot;;
$mqttP2PTopic = $clientId;
$ZPL = $deviceId.&quot;|1007|2019022611153312312345|566.88|2001&quot;;
echo date('Y-m-d H:i:s').&quot; 推送Topic:&quot;.$mqttP2PTopic.&quot;\n&quot;;
echo date('Y-m-d H:i:s').&quot; 推送内容:&quot;.$ZPL.&quot;\n&quot;;
$mqttClient-&amp;publish($mqttP2PTopic, $ZPL, $qos);
## 打印机需要发送十六进制数组,需要对hex数据pack操作
## 例如打印内容为:$ZPL = &quot;1E1003000a414243440d0a1B636f90&quot;;
## 则需要$mqttClient-&amp;publish($mqttP2PTopic, pack(&quot;H*&quot;,$ZPL), $qos);
});
## 设置发送成功回调
$mqttClient-&amp;onPublish(function ($publishedId) use ($mqttClient, $mid) {
echo date('Y-m-d H:i:s').&quot; 发送成功:publish message success &quot; . $mid . &quot;\n&quot;;
});
## 设置消息接收回调
$mqttClient-&amp;onMessage(function ($message) {
echo date('Y-m-d H:i:s').&quot; 接收回调:Message From mqtt, topic is &quot; . $message-&amp;topic . &quot; qos is &quot; . $message-&amp;qos . &quot; messageId is &quot; . $message-&amp;mid . &quot; payload is &quot; . $message-&amp;payload . &quot;\n&quot;;
});
$mqttClient-&amp;connect($endPoint, $port, $keepalive);
$mqttClient-&amp;loopForever();
echo &quot;Finished&quot;;</code></pre>