移动支付产品接入

移动支付产品接入文档


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 -&amp;gt;setUsername($username) -&amp;gt;setPassword($password) -&amp;gt;setKeepAliveInterval(60) // Last Will 设置 -&amp;gt;setLastWillTopic(设备IMEI) -&amp;gt;setLastWillMessage('client disconnect') -&amp;gt;setLastWillQualityOfService(1);</code></pre> <h5>三、订阅消息</h5> <p>编写代码订阅 emqx/test 主题,并为该订阅配置回调函数以处理接收到的消息,此处我们将订阅得到的主题和消息打印出来:</p> <pre><code class="language-php">// 订阅 $mqtt-&amp;gt;subscribe('emqx/test', function ($topic, $message) { printf(&amp;quot;Received message on topic [%s]: %s\n&amp;quot;, $topic, $message); }, 0); </code></pre> <h4>四、发布消息</h4> <p>构造一个 payload,调用 publish 函数向 emqx/test 主题发布消息,发布完成之后客户端需要进入轮询状态,处理传入的消息和重发队列:</p> <pre><code class="language-php">for ($i = 0; $i&amp;lt; 10; $i++) { $payload = array( 'protocol' =&amp;gt; 'tcp', 'date' =&amp;gt; date('Y-m-d H:i:s'), 'url' =&amp;gt; 'https://github.com/emqx/MQTT-Client-Examples' ); $mqtt-&amp;gt;publish( // topic 'emqx/test', // payload json_encode($payload), // qos 0, // retain true ); printf(&amp;quot;msg $i send\n&amp;quot;); sleep(1); } // 客户端轮询以处理传入消息和重发队列 $mqtt-&amp;gt;loop(true); // 断开 MQTT 连接 // $client-&amp;gt;disconnect(); </code></pre> <h4>完整代码</h4> <p>服务器连接、消息发布与接收代码:</p> <pre><code class="language-php">&amp;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 -&amp;gt;setUsername($username) -&amp;gt;setPassword(null) -&amp;gt;setKeepAliveInterval(60) // Last Will 设置 -&amp;gt;setLastWillTopic('emqx/test/last-will') -&amp;gt;setLastWillMessage('client disconnect') -&amp;gt;setLastWillQualityOfService(1); $mqtt = new MqttClient($server, $port, $clientId); $mqtt-&amp;gt;connect($connectionSettings, $clean_session); printf(&amp;quot;client connected\n&amp;quot;); $mqtt-&amp;gt;subscribe('emqx/test', function ($topic, $message) { printf(&amp;quot;Received message on topic [%s]: %s\n&amp;quot;, $topic, $message); }, 0); for ($i = 0; $i&amp;lt; 10; $i++) { $payload = array( 'protocol' =&amp;gt; 'tcp', 'date' =&amp;gt; date('Y-m-d H:i:s'), 'url' =&amp;gt; 'https://github.com/emqx/MQTT-Client-Examples' ); $mqtt-&amp;gt;publish( // topic 'emqx/test', // payload json_encode($payload), // qos 0, // retain true ); printf(&amp;quot;msg $i send\n&amp;quot;); sleep(1); } $mqtt-&amp;gt;loop(true); // 断开 MQTT 连接 // $client-&amp;gt;disconnect(); </code></pre> <pre><code class="language-php">&amp;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 . &amp;quot;\n&amp;quot;; $mid = 0; $mqttClient = new Mosquitto\Client($masterClientId, $cleanSession); echo &amp;quot;UserName:&amp;quot; . $username . &amp;quot; Password:&amp;quot; . $password . &amp;quot;\n&amp;quot;; $mqttClient-&amp;amp;setCredentials($username, $password); ## 设置连接成功回调 $mqttClient-&amp;amp;onConnect(function ($rc, $message) use ($mqttClient, &amp;amp;amp;$mid, $topic, $qos, $clientId) { echo date('Y-m-d H:i:s').&amp;quot; 连接成功:Connnect to Server Code is &amp;quot; . $rc . &amp;quot; message is &amp;quot; . $message . &amp;quot;\n&amp;quot;; $mqttP2PTopic = $clientId; $ZPL = $deviceId.&amp;quot;|1007|2019022611153312312345|566.88|2001&amp;quot;; echo date('Y-m-d H:i:s').&amp;quot; 推送Topic:&amp;quot;.$mqttP2PTopic.&amp;quot;\n&amp;quot;; echo date('Y-m-d H:i:s').&amp;quot; 推送内容:&amp;quot;.$ZPL.&amp;quot;\n&amp;quot;; $mqttClient-&amp;amp;publish($mqttP2PTopic, $ZPL, $qos); ## 打印机需要发送十六进制数组,需要对hex数据pack操作 ## 例如打印内容为:$ZPL = &amp;quot;1E1003000a414243440d0a1B636f90&amp;quot;; ## 则需要$mqttClient-&amp;amp;publish($mqttP2PTopic, pack(&amp;quot;H*&amp;quot;,$ZPL), $qos); }); ## 设置发送成功回调 $mqttClient-&amp;amp;onPublish(function ($publishedId) use ($mqttClient, $mid) { echo date('Y-m-d H:i:s').&amp;quot; 发送成功:publish message success &amp;quot; . $mid . &amp;quot;\n&amp;quot;; }); ## 设置消息接收回调 $mqttClient-&amp;amp;onMessage(function ($message) { echo date('Y-m-d H:i:s').&amp;quot; 接收回调:Message From mqtt, topic is &amp;quot; . $message-&amp;amp;topic . &amp;quot; qos is &amp;quot; . $message-&amp;amp;qos . &amp;quot; messageId is &amp;quot; . $message-&amp;amp;mid . &amp;quot; payload is &amp;quot; . $message-&amp;amp;payload . &amp;quot;\n&amp;quot;; }); $mqttClient-&amp;amp;connect($endPoint, $port, $keepalive); $mqttClient-&amp;amp;loopForever(); echo &amp;quot;Finished&amp;quot;;</code></pre>

页面列表

ITEM_HTML