Mosquitto-PHP

一个用于 PHP 的 Eclipse Mosquitto™ MQTT 客户端库的封装器。「A wrapper for the Eclipse Mosquitto™ MQTT client library for PHP.」

Github星跟踪图

Mosquitto-PHP

这是一个允许在 PHP 中使用 Eclipse Mosquitto™ MQTT 客户端库的扩展。使用方法请参见 examples/ 目录。

支持PHP 7

感谢 Sara Golemon,这个扩展现在支持 PHP 7 了。如果有使用 PHP 7 的人能够测试它并让我知道它是如何工作的,我将非常感激。

要求

  • PHP 5.3以上
  • libmosquitto 1.2.x 或更高版本。
  • 我手头没有 Windows 机器,当然也非常欢迎打补丁或提出请求。

安装方法

如果你使用了一个预建包来安装 Mosquitto,你需要确保你已经安装了开发头文件。在 Red Hat 衍生的系统上,这可能叫做 libmosquitto-devel,而在基于 Debian 的系统上,它将是 libmosquitto-dev。

你可以使用 PECL 获得这个软件包。

pecl install Mosquitto-alpha

或者,你也可以使用普通的扩展程序。

phpize
./configure --with-mosquitto=/path/to/libmosquitto
make
make install

然后在你的 php.ini 中添加 extension=mosquitto.so。

--with-mosquitto 参数是可选的,只有当你的 libmosquitto 安装失败时才需要。

一般操作

底层库是基于回调和异步操作的。因此,你必须经常调用 Clien t的 loop() 方法,以允许库处理其队列中的消息。另外,你应该使用回调函数来确保只有在客户端连接后才尝试发布等。例如,以下是你如何正确发布一个 QoS=2 的消息。

onLog('var_dump');
$c->onConnect(function() use ($c, &$mid) {
    $mid = $c->publish("mgdm/test", "Hello", 2);
});

$c->onPublish(function($publishedId) use ($c, $mid) {
    if ($publishedId == $mid) {
        $c->disconnect();
    }
});

$c->connect("localhost");
$c->loopForever();

echo "Finished"

文档

完整的文档可以在 available on ReadTheDocs 上找到。


主要指标

概览
名称与所有者angular/angular-cn
主编程语言Pug
编程语言PHP (语言数: 11)
平台Linux, Mac
许可证MIT License
所有者活动
创建于2016-05-26 19:07:10
推送于2022-10-29 06:47:25
最后一次提交2021-12-11 08:36:46
发布数512
最新版本名称12.0.0-rc.2 (发布于 )
第一版名称2.0.0-alpha.13 (发布于 )
用户参与
星数757
关注者数43
派生数423
提交数4k
已启用问题?
问题数226
打开的问题数5
拉请求数219
打开的拉请求数1
关闭的拉请求数60
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?

Mosquitto-PHP

This is an extension to allow using the Eclipse Mosquitto™ MQTT client library with PHP. See the examples/ directory for usage.

Build Status

PHP 7 support

Thanks to Sara Golemon this extension now supports PHP 7. I would be grateful if anyone using PHP 7 could test it and let me know how it works out.

Requirements

  • PHP 5.3+
  • libmosquitto 1.2.x or later
  • Linux or Mac OS X. I do not have a Windows machine handy, though patches or
    pull requests are of course very welcome!

Installation

If you've used a pre-built package to install Mosquitto, you need to make sure you have the development headers installed. On Red Hat-derived systems, this is probably called libmosquitto-devel, and on Debian-based systems it will be libmosquitto-dev.

You may obtain this package using PECL:

pecl install Mosquitto-alpha

Alternatively, you can use the normal extension build process:

phpize
./configure --with-mosquitto=/path/to/libmosquitto
make
make install

Then add extension=mosquitto.so to your php.ini.

The --with-mosquitto argument is optional, and only required if your
libmosquitto install cannot be found.

General operation

The underlying library is based on callbacks and asynchronous operation. As such, you have to call the loop() method of the Client frequently to permit the library to handle the messages in its queues. Also, you should use the callback functions to ensure that you only attempt to publish after the client has connected, etc. For example, here is how you would correctly publish a QoS=2 message:

<?php

use Mosquitto\Client;

$mid = 0;
$c = new Mosquitto\Client("PHP");
$c->onLog('var_dump');
$c->onConnect(function() use ($c, &$mid) {
    $mid = $c->publish("mgdm/test", "Hello", 2);
});

$c->onPublish(function($publishedId) use ($c, $mid) {
    if ($publishedId == $mid) {
        $c->disconnect();
    }
});

$c->connect("localhost");
$c->loopForever();

echo "Finished"

Documentation

Full documentation is available on ReadTheDocs.