我正在尝试从我的 Mosquitto 代理加载消息,该代理使用 certbot 证书进行保护(并通过 MQTTX 进行测试),但在尝试制作 php-MQTT /client 库并在网络上搜索很多并询问聊天 GPT 后,我不得不认输直到知道。我发现的各种示例供本地使用或自签名证书使用。 我可以通过 Javascript 和 paho 库连接到我的代理,但我真的很想做服务器端的大部分工作:将有效负载存储在 MYSQL 数据库中,加载消息并准备它们,然后将 javascript 用于我的前端。 关于我做错了什么的建议将不胜感激。另外,如果我使用 php 库与 MQTT 一起工作的方向错误,我很感激。我对整个 MQTT 和 API 世界还很陌生。
这是我当前使用 PHP 8.1 的设置: 我检查了文件权限和服务器可以访问它们
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;
// Define your MQTT broker host, port, and TLS port.
$host = 'www.example.com';
$port = 1883;
$tlsPort = 8883;
// Define the path to your certificate files.
$caFile = '/etc/mosquitto/certs/chain.pem';
$certFile = '/etc/mosquitto/certs/fullchain.pem';
$keyFile = '/etc/mosquitto/certs/privkey.pem';
$keyPassphrase = null;
// Define your MQTT authorization credentials.
$username = 'xxxxx';
$password = 'xxxxxx';
// Create and configure the connection settings.
$connectionSettings = (new ConnectionSettings)
->setUseTls(true)
->setTlsCertificateAuthorityFile($caFile)
->setTlsClientCertificateFile($certFile)
->setTlsClientCertificateKeyFile($keyFile)
->setTlsClientCertificateKeyPassphrase($keyPassphrase)
->setUsername($username)
->setPassword($password)
->setTlsVerifyPeerName(false); //I tried true as well no difference
// Create the MQTT client instance.
$client = new MqttClient($host, $port, 'php-mqtt-client');
// Enable error logging
ini_set('log_errors', '1');
ini_set('error_log', 'php_errors.log');
// Set the error reporting level to show all errors
error_reporting(E_ALL);
// Set up an error handler to log errors
set_error_handler(function($errno, $errstr, $errfile, $errline) {
error_log("Error [$errno]: $errstr in $errfile on line $errline");
});
ini_set('display_errors', '1');
// Connect to the MQTT broker using the configured connection settings.
$client->connect($connectionSettings);
// Subscribe to a topic.
$client->subscribe('topic', function ($topic, $message, $retained, $matchedWildcards) {
echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);
$client->loop(true);
// Disconnect from the MQTT broker.
$client->disconnect();
错误日志返回:
[26-Mar-2023 15:32:44 UTC] Error [2]: stream_socket_enable_crypto(): SSL: Connection reset by peer in /var/www/www.etd-data.nl/vendor/php-mqtt/client/src/MqttClient.php on line 266
[26-Mar-2023 15:32:44 UTC] PHP Fatal error: Uncaught PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException: [2000] Establishing a connection to the MQTT broker failed: TLS error [UNKNOWN:1]: Unknown error in /var/www/www.etd-data.nl/vendor/php-mqtt/client/src/MqttClient.php:284
Stack trace:
#0 /var/www/www.etd-data.nl/vendor/php-mqtt/client/src/MqttClient.php(158): PhpMqtt\Client\MqttClient->establishSocketConnection()
#1 /var/www/www.etd-data.nl/vendor/php-mqtt/client/src/MqttClient.php(144): PhpMqtt\Client\MqttClient->connectInternal()
#2 /var/www/www.etd-data.nl/test.php(60): PhpMqtt\Client\MqttClient->connect()
#3 {main}
thrown in /var/www/www.etd-data.nl/vendor/php-mqtt/client/src/MqttClient.php on line 284