Home>Article>Topics> How to install the Mosquitto-php extension under the Pagoda panel

How to install the Mosquitto-php extension under the Pagoda panel

藏色散人
藏色散人 forward
2021-03-24 16:14:26 3466browse

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

How to install the Mosquitto-php extension under the Pagoda panel

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

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

我们用 PECL 来安装 Mosquitto 的 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:

453788c47baf2e3a2a2ba5e3aa610e97setCredentials($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 2ce72521d1c44377256fa5da66fe68e5loop(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 above is the detailed content of How to install the Mosquitto-php extension under the Pagoda panel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete