当前位置:首页 > PHP

php实现mqtt

2026-03-12 20:16:36PHP

PHP 实现 MQTT 通信

MQTT(Message Queuing Telemetry Transport)是一种轻量级的发布/订阅消息传输协议,常用于物联网(IoT)场景。PHP 可以通过第三方库实现 MQTT 通信。

使用 php-mqtt/client

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

php实现mqtt

安装库:

php实现mqtt

composer require php-mqtt/client

发布消息示例

<?php
require __DIR__ . '/vendor/autoload.php';

use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;

$server   = 'broker.example.com';
$port     = 1883;
$clientId = 'php-publisher';
$username = 'username';
$password = 'password';
$topic    = 'test/topic';
$message  = 'Hello MQTT!';

$mqtt = new MqttClient($server, $port, $clientId);

$connectionSettings = (new ConnectionSettings)
    ->setUsername($username)
    ->setPassword($password);

$mqtt->connect($connectionSettings, true);
$mqtt->publish($topic, $message, 0);
$mqtt->disconnect();
?>

订阅消息示例

<?php
require __DIR__ . '/vendor/autoload.php';

use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;

$server   = 'broker.example.com';
$port     = 1883;
$clientId = 'php-subscriber';
$username = 'username';
$password = 'password';
$topic    = 'test/topic';

$mqtt = new MqttClient($server, $port, $clientId);

$connectionSettings = (new ConnectionSettings)
    ->setUsername($username)
    ->setPassword($password);

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

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

使用 bluerhinos/phpmqtt

另一个可选库是 bluerhinos/phpmqtt,适用于较旧的 PHP 版本。

安装库:

composer require bluerhinos/phpmqtt

发布消息示例

<?php
require __DIR__ . '/vendor/autoload.php';

use Bluerhinos\phpMQTT;

$server = 'broker.example.com';
$port   = 1883;
$clientId = 'php-publisher';
$username = 'username';
$password = 'password';
$topic    = 'test/topic';
$message  = 'Hello MQTT!';

$mqtt = new phpMQTT($server, $port, $clientId);

if ($mqtt->connect(true, NULL, $username, $password)) {
    $mqtt->publish($topic, $message, 0);
    $mqtt->close();
}
?>

订阅消息示例

<?php
require __DIR__ . '/vendor/autoload.php';

use Bluerhinos\phpMQTT;

$server = 'broker.example.com';
$port   = 1883;
$clientId = 'php-subscriber';
$username = 'username';
$password = 'password';
$topic    = 'test/topic';

$mqtt = new phpMQTT($server, $port, $clientId);

if ($mqtt->connect(true, NULL, $username, $password)) {
    $topics[$topic] = ['qos' => 0, 'function' => function ($topic, $msg) {
        echo "Received message on topic [$topic]: $msg\n";
    }];
    $mqtt->subscribe($topics, 0);

    while ($mqtt->proc()) {}

    $mqtt->close();
}
?>

注意事项

  • 确保 PHP 环境支持 socket 扩展。
  • MQTT 服务器地址、端口、认证信息需根据实际环境配置。
  • 生产环境中建议使用 TLS/SSL 加密连接。
  • 对于长时间运行的订阅脚本,建议使用 supervisor 等工具管理进程。

标签: phpmqtt
分享给朋友:

相关文章

php实现购物车

php实现购物车

PHP实现购物车功能 购物车功能是电商网站的核心模块之一,通常涉及商品添加、删除、数量修改和结算等操作。以下是使用PHP实现购物车功能的几种方法。 使用Session存储购物车数据 Session是…

php实现mysql

php实现mysql

PHP 连接 MySQL 的基本方法 PHP 提供了多种方式与 MySQL 数据库进行交互,以下是最常用的几种方法。 使用 MySQLi 扩展 MySQLi 是 MySQL 的改进扩展,支持面向对…

php搜索功能实现

php搜索功能实现

实现基本的PHP搜索功能 使用MySQL数据库和PHP实现简单的关键词搜索功能。需要确保数据库中有可供搜索的表和数据。 // 连接数据库 $conn = new mysqli('localhost'…

php实现博客

php实现博客

使用PHP实现博客系统 数据库设计 创建数据库表存储博客文章和用户信息。常见的表包括posts(文章)和users(用户)。 CREATE TABLE users ( id INT AUTO…

php 无限分类的实现

php 无限分类的实现

无限分类的实现方法 无限分类通常用于构建树形结构数据,如商品分类、多级菜单等。以下是几种常见的实现方式: 邻接列表模型(Adjacency List) 邻接列表是最简单的实现方式,通过在每个节点中存…

php 实现聊天功能

php 实现聊天功能

PHP 实现聊天功能的方法 使用 WebSocket 和 Ratchet 库 WebSocket 是实现实时聊天的理想选择。Ratchet 是一个 PHP 库,用于处理 WebSocket 连接。…