当前位置:首页 > PHP

php mqtt 实现

2026-02-14 14:39:06PHP

PHP 实现 MQTT 通信的方法

使用 PHP 实现 MQTT 通信可以通过第三方库或扩展完成,以下是常见的实现方式:

使用 php-mqtt/client

php-mqtt/client 是一个纯 PHP 实现的 MQTT 客户端库,支持 MQTT 3.1 和 3.1.1 协议。

安装依赖:

composer require php-mqtt/client

发布消息示例:

<?php
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'broker.example.com';
$port     = 1883;
$clientId = 'php-publisher';
$username = 'user';
$password = 'pass';

$mqtt = new MqttClient($server, $port, $clientId);
$connectionSettings = (new ConnectionSettings)
    ->setUsername($username)
    ->setPassword($password);

$mqtt->connect($connectionSettings, true);
$mqtt->publish('topic/test', 'Hello World!', 0);
$mqtt->disconnect();

订阅消息示例:

php mqtt 实现

<?php
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'broker.example.com';
$port     = 1883;
$clientId = 'php-subscriber';
$username = 'user';
$password = 'pass';

$mqtt = new MqttClient($server, $port, $clientId);
$connectionSettings = (new ConnectionSettings)
    ->setUsername($username)
    ->setPassword($password);

$mqtt->connect($connectionSettings, true);
$mqtt->subscribe('topic/test', function ($topic, $message) {
    echo "Received message on topic [$topic]: $message\n";
}, 0);

$mqtt->loop(true);
$mqtt->disconnect();

使用 Mosquitto PHP 扩展

Mosquitto 是一个流行的 MQTT 代理,其 PHP 扩展提供了原生支持。

安装扩展:

pecl install Mosquitto-alpha

发布消息示例:

php mqtt 实现

<?php
$client = new Mosquitto\Client('php-publisher');
$client->setCredentials('user', 'pass');
$client->connect('broker.example.com', 1883, 60);

$client->onConnect(function() use ($client) {
    $client->publish('topic/test', 'Hello World!', 0, false);
});

$client->loopForever();

订阅消息示例:

<?php
$client = new Mosquitto\Client('php-subscriber');
$client->setCredentials('user', 'pass');
$client->connect('broker.example.com', 1883, 60);

$client->onMessage(function($message) {
    printf("Received message on topic [%s]: %s\n", 
        $message->topic, $message->payload);
});

$client->subscribe('topic/test', 0);
$client->loopForever();

使用 Bluerhinos/phpMQTT

这是一个轻量级的 PHP MQTT 客户端库,适合简单场景。

安装依赖:

composer require bluerhinos/phpmqtt

基本使用示例:

<?php
require('phpMQTT.php');

$server = 'broker.example.com'; 
$port   = 1883;
$username = 'user';
$password = 'pass';
$client_id = 'php-client';

$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
if ($mqtt->connect(true, NULL, $username, $password)) {
    $mqtt->publish('topic/test', 'Hello World!', 0);
    $mqtt->close();
} else {
    echo "Connection failed\n";
}

注意事项

  • 确保 MQTT 代理服务器(如 Mosquitto、EMQX 等)已正确配置并运行
  • 生产环境建议使用 TLS/SSL 加密连接
  • 对于长时间运行的订阅客户端,需要处理连接断开和重连逻辑
  • 考虑使用 QoS 等级(0/1/2)根据业务需求平衡可靠性和性能

以上方法可根据项目需求选择,php-mqtt/client 功能最全面,Mosquitto 扩展性能最佳,phpMQTT 最轻量。

标签: phpmqtt
分享给朋友:

相关文章

php实现验证码

php实现验证码

PHP实现验证码的方法 使用GD库生成验证码 GD库是PHP中处理图像的扩展,可用于生成验证码图片。 <?php session_start(); $width = 120; $height…

php实现增删改查

php实现增删改查

以下是在PHP中实现增删改查(CRUD)操作的详细方法,以MySQL数据库为例: 数据库连接配置 创建config.php文件存储数据库连接信息: <?php $host = 'localh…

php 路由实现

php 路由实现

PHP 路由实现方法 在 PHP 中实现路由功能有多种方式,以下是几种常见的实现方法: 使用原生 PHP 实现 通过解析 URL 并匹配对应的控制器和动作: $request = $_SERV…

php 实现验证码

php 实现验证码

使用 PHP 生成验证码 验证码(CAPTCHA)用于防止自动化脚本提交表单,以下是几种常见的 PHP 实现方法。 使用 GD 库生成图片验证码 GD 库是 PHP 的图像处理扩展,可用于生成验证码…

php实现分类

php实现分类

PHP实现分类的方法 数据库设计 创建分类表时,通常需要包含id、名称、父级id等字段。父级id用于实现多级分类结构。 CREATE TABLE categories ( id INT AU…

php实现即时通讯

php实现即时通讯

PHP 实现即时通讯的方案 即时通讯(IM)可以通过多种方式实现,PHP 作为服务器端语言,通常需要结合前端技术(如 WebSocket、AJAX)或第三方服务来实现实时通信。以下是几种常见的实现方式…