宝塔面板下怎么安装Mosquitto-php扩展

下面由宝塔教程栏目给大家介绍宝塔面板下安装mosquitto-php扩展方法,希望对需要的朋友有所帮助!

宝塔面板下怎么安装Mosquitto-php扩展

MQTT 是物联网的消息传送协议标准。

centos 7上常用的开源 MQTT 消息服务器就是 Mosquitto。

我们用 PECL 来安装 Mosquitto 的 PHP 实现。

立即学习PHP免费学习笔记(深入)”;

首先要确保 php-devel 已经安装:

# yum install -y php-devel

然后确保 mosquitto-devel 也已经安装:

yum install -y mosquitto-devel

由于宝塔是多环境共存,以下以PHP7.1为例
再用 pecl 来安装 Mosquitto-PHP

# /www/server/php/71/bin/pecl install Mosquitto-alpha

然后去 /www/server/php/71/etc/php.ini 添加一行:

extension=mosquitto.so

重启 服务后,运行 php -i|grep mosquitto 可以看到  
mosquitto  
libmosquitto version => 1.4.13

表明 PHP 可以使用 mosquitto 的 MQTT 库了。

下面是用 PHP 来发送消息的示例代码 mosquitto-test.php:

<?php   2   3   4 $c = new MosquittoClient;   5   6 $topic = 'test';   7 $msg = 'hello你好';   8 $qos = 2;   9   10 $username = 'username';   11 $password = 'Passw0rd';   12   13 $c->setCredentials($username, $password);   14 $c->onConnect(function() use ($c) {   15 global $topic,$msg,$qos;   16 $c->publish($topic, $msg, $qos);   17 });   18   19 $host = '192.168.7.201';   20   21 $c->connect($host);   22 for ($i = 0; $i < 100; $i++) {   23 // Loop around to permit the library to do its work   24 $c->loop(1);   25 }   26   27 echo "结束n";

然后运行 php mosqitto-test.php

另外开启一个终端,用

# mosquitto_sub -h 192.168.7.201 -u username -P Passw0rd -t test

可以看到 PHP 程序发送的消息: “hello你好”.

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享