Wallogit.com
2017 © Pedro Peláez
MQTT 3.1.1 library for PHP with TLS support
MQTT 3.1.1 Client with TSL support in PHP, (*1)
Note that all calls are blocking until a timeout occurs. If you need some fancy async solution, you'll have to find another repo., (*2)
This implementation works with MQTT ver 3.1.1 for QOS levels 0 and 1. QOS level 2 should work, but is experimental until better tested., (*3)
The preferred way to install this extension is through composer., (*4)
Either run, (*5)
php composer.phar require --prefer-dist karpy47/php-mqtt-client
or add, (*6)
"karpy47/php-mqtt-client": "*"
to the require section of your composer.json., (*7)
Should work with all recent PHP versions., (*8)
Code developed and running in production using PHP v7.0.27 (previously also PHP v5.6.27), (*9)
use karpy47\PhpMqttClient\MQTTClient;
$client = new MQTTClient('mqtt-server.domain.com', 8162);
$client->setAuthentication('mqtt-server.username','mqtt-server.password');
$client->setEncryption('cacerts.pem');
$success = $client->sendConnect(12345); // set your client ID
if ($success) {
$client->sendSubscribe('topic1');
$client->sendPublish('topic2', 'Message to all subscribers of this topic');
$messages = $client->getPublishMessages(); // now read and acknowledge all messages waiting
foreach ($messages as $message) {
echo $message['topic'] .': '. $message['message'] . PHP_EOL;
// Other keys in $message array: retain (boolean), duplicate (boolean), qos (0-2), packetId (2-byte integer)
}
$client->sendDisconnect();
}
$client->close();
Thanks to bluerhinos/phpMQTT and McFizh/libMQTT., (*10)
Also thanks to * pascalwacker for forking and fixing some bugs * tobbexiv for finding and fixing several bugs, (*11)
Released under the MIT License. Please see License File for more information., (*12)