Home> Topics> Pagoda Panel> body text

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

藏色散人
Release: 2021-03-24 16:14:26
forward
3444 people have browsed it

下面由宝塔教程栏目给大家介绍宝塔面板下安装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
Copy after login

然后确保 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:

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";
Copy after login

然后运行 php mosqitto-test.php

另外开启一个终端,用

# mosquitto_sub -h 192.168.7.201 -u username -P Passw0rd -t test
Copy after login

可以看到 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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!