当前位置:首页 > PHP

php实现mqtt

2026-02-14 21:37:53PHP

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 = 'username';
$password = 'password';

$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
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

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

$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();

使用 bluerhinos/phpmqtt

另一个常用库是 bluerhinos/phpmqtt,这是一个纯 PHP 实现的 MQTT 客户端。

安装依赖:

composer require bluerhinos/phpmqtt

发布消息示例:

<?php
require('vendor/autoload.php');
use Bluerhinos\phpMQTT;

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

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

订阅消息示例:

<?php
require('vendor/autoload.php');
use Bluerhinos\phpMQTT;

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

$mqtt = new phpMQTT($server, $port, $client_id);
if(!$mqtt->connect(true, NULL, $username, $password)) {
    exit(1);
}

$topics['topic/test'] = array('qos' => 0, 'function' => 'procmsg');
$mqtt->subscribe($topics, 0);

function procmsg($topic, $msg) {
    echo "Msg Recieved: {$msg}\n";
}

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

使用 Mosquitto 扩展

PHP 也可以使用 Mosquitto 扩展来连接 MQTT 代理。

安装 Mosquitto 扩展:

pecl install Mosquitto-alpha

发布消息示例:

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

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

$client->loopForever();

订阅消息示例:

php实现mqtt

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

$client->onConnect(function() use ($client) {
    $client->subscribe('topic/test', 0);
});

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

$client->loopForever();

注意事项

  • 确保 MQTT 代理服务器地址、端口、用户名和密码正确
  • 根据需求选择合适的 QoS 等级(0、1 或 2)
  • 长时间运行的订阅脚本需要处理连接断开和重连逻辑
  • 生产环境建议添加错误处理和日志记录

标签: phpmqtt
分享给朋友:

相关文章

php实现递归

php实现递归

递归的基本概念 递归是一种函数调用自身的技术,适用于解决可以分解为相同子问题的问题。在PHP中,递归需要明确的终止条件(基线条件),否则会导致无限循环。 递归实现阶乘 阶乘是经典的递归案例,n! =…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注意…

php怎么实现登录功能

php怎么实现登录功能

数据库准备 创建用户表存储登录信息,通常包括用户名、密码(需加密)等字段。示例SQL: CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY…

php 实现下载

php 实现下载

PHP 实现文件下载的方法 使用 PHP 实现文件下载可以通过设置 HTTP 头信息,强制浏览器将文件作为附件下载而非直接打开。 基本下载实现 $file_path = 'path/to/your/…

php 实现跳转

php 实现跳转

PHP 实现页面跳转的方法 在PHP中,实现页面跳转可以通过多种方式完成,以下是几种常用的方法: 使用header函数 header函数是PHP中最常用的跳转方法,通过发送HTTP头部信息实现跳转。…

php 链表实现

php 链表实现

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。 链表节点类(Node) 链表的基本单元是节…