php-mqtt/client

PHP Fatal error: Uncaught Error: Class "PhpMqtt\shared\SimpleLogger"

tissi-2 opened this issue · 1 comments

Hello everybody! I am getting the PHP Fatal error: Uncaught Error: Class "PhpMqtt\shared\SimpleLogger". I would like to know the exact folder and file structure to get it working since some files are only to be found in the examples files of php-mqtt/client-examples and I am not allowed to use composer to install this package. Could somebody explain the namespace function and the purpose of the config.php?

Thank you very much!

You don't need any of this. These files are only in the examples to simplify configuration for all examples at once (config.php) and to provide detailed output to the user (SimpleLogger.php).

The Publish with QoS 0 example without any additional dependencies looks like this:

<?php

declare(strict_types=1);

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

use PhpMqtt\Client\Examples\Shared\SimpleLogger;
use PhpMqtt\Client\Exceptions\MqttClientException;
use PhpMqtt\Client\MqttClient;
use Psr\Log\LogLevel;

$brokerHost = 'test.mosquitto.org';
$brokerPort = 1883;

try {
    // Create a new instance of an MQTT client and configure it to use the shared broker host and port.
    $client = new MqttClient($brokerHost, $brokerPort, 'test-publisher', MqttClient::MQTT_3_1);

    // Connect to the broker without specific connection settings but with a clean session.
    $client->connect(null, true);

    // Publish the message 'Hello world!' on the topic 'foo/bar/baz' using QoS 0.
    $client->publish('foo/bar/baz', 'Hello world!', MqttClient::QOS_AT_MOST_ONCE);

    // Gracefully terminate the connection to the broker.
    $client->disconnect();
} catch (MqttClientException $e) {
    // MqttClientException is the base exception of all exceptions in the library. Catching it will catch all MQTT related exceptions.
    echo 'Publishing a message using QoS 0 failed. An exception occurred: ' . $e->getMessage();
}

Using this library without composer is discouraged though and I cannot provide any support for it. Since the library depends on other libraries itself, it is also quite the pain to update if you do so.