云打印-PHP阿里云MQTT推送代码
<pre><code class="language-php">&lt;?php
use Mosquitto\Client;
##此处填写阿里云帐号 AccessKey
$accessKey = 'LTAI4FgxuFC1auJcZNSnFSw5';
##此处填写阿里云帐号 SecretKey
$secretKey = 'uRYSk7pGXkGEE1uNnezShLbvBkAC6v';
## 接入点地址,购买实例后从控制台获取
$endPoint = 'mqtt-cn-0pp1bnxar02.mqtt.aliyuncs.com';
##实例 ID,购买后从控制台获取
$instanceId = 'mqtt-cn-0pp1bnxar02';
## MQTT 第一级 Topic 需要在 MQTT 控制台提前申请
$topic = 'TID_ZKC';
## MQTT 客户端ID 前缀, GroupID,需要在 MQTT 控制台申请
$groupId = 'GID_ZKC';
## MQTT 客户端ID 后缀,DeviceId,业务方自由指定,需要保证全局唯一,禁止 2 个客户端连接使用同一个 ID
## 设备IMEI号码
$deviceId = '352736082691067';
## QOS必须为2
$qos = 2;
$port = 1883;
$keepalive = 90;
$cleanSession = true;
$clientId = $groupId . '@@@' . $deviceId;
## 发送端ClientID,@@@后面字符串可以随意修改,不能与设备IMEI重复
$masterClientId=$groupId . '@@@20191010master';
echo $clientId . &quot;\n&quot;;
$mid = 0;
## 初始化客户端,需要设置 clientId 和 CleanSession 参数,参考官网文档规范
$mqttClient = new Mosquitto\Client($masterClientId, $cleanSession);
## 设置鉴权参数,参考 MQTT 客户端鉴权代码计算 username 和 password
$username = 'Signature|' . $accessKey . '|' . $instanceId;
$sigStr = hash_hmac(&quot;sha1&quot;, $masterClientId, $secretKey, true);
$password = base64_encode($sigStr);
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;;
## P2P消息的二级 topic 是/p2p/,三级 topic 是目标客户端的 clientId
$mqttP2PTopic = $topic . &quot;/p2p/&quot; . $clientId;
$printText=&quot;ABCD\r\n&quot;;//打印内容
$ZPL=bin2hex($printText);
$hexBin=pack(&quot;H*&quot;,$ZPL);
$len=strlen($hexBin)+4;//打印指定尾部4字节
$lenHex=dechex($len);
$len_MSB=dechex(($len&amp;0xff00)&gt;&gt;8);
$len_LSB=dechex($len&amp;0x00ff);
$lenMSBStr=str_pad($len_MSB,2,0,STR_PAD_LEFT);
$lenLSBSTR=str_pad($len_LSB,2,0,STR_PAD_LEFT);
echo &quot;数据长度:&quot;.$len;
echo &quot;\r最高位是&quot;.$lenMSBStr;
echo &quot;\r最低位是&quot;.$lenLSBSTR;
$printHex=&quot;1E1001&quot;.$lenMSBStr.$lenLSBSTR.$ZPL.&quot;1B636F90&quot;;
echo &quot;\r最终打印内容,请转HEX:&quot;.$printHex;
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;.$printHex.&quot;\n&quot;;
$mqttClient-&amp;publish($mqttP2PTopic, pack(&quot;H*&quot;,$printHex), $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>