PHP code example of kokx / mqtt

1. Go to this page and download the library: Download kokx/mqtt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kokx / mqtt example snippets

 php


use MQTT\Client as MQTTClient;

$client = new MQTTClient([
    'hostname' => 'localhost'
]);

$client->connect();

$client->subscribe('state/device/#', function ($topic, $payload) {
    echo "Received message on topic $topic with payload $payload";
});

// repetetively call loop() to keep receiving messages
while (true) {
    $client->loop();
    usleep(50000);
}