32 lines
1.0 KiB
PHP
32 lines
1.0 KiB
PHP
<?php
|
|
require __DIR__.'/vendor/autoload.php';
|
|
use PhpMqtt\Client\MqttClient;
|
|
use PhpMqtt\Client\ConnectionSettings;
|
|
|
|
$config = require __DIR__.'/config.php';
|
|
|
|
$topic = $_POST['topic'] ?? $_GET['topic'] ?? null;
|
|
$msg = $_POST['msg'] ?? $_GET['msg'] ?? null;
|
|
$qos = (int)($_POST['qos'] ?? 0);
|
|
$retain = (bool)($_POST['retain'] ?? false);
|
|
|
|
if (!$topic || $msg === null) {
|
|
http_response_code(400);
|
|
die('missing topic or msg');
|
|
}
|
|
|
|
$settings = (new ConnectionSettings)
|
|
->setUsername($config['username'])
|
|
->setPassword($config['password'])
|
|
->setKeepAliveInterval($config['keepalive'])
|
|
->setConnectTimeout($config['timeout'])
|
|
->setUseTls(false); // muda para true quando fores usar 8883/TLS
|
|
|
|
$client = new MqttClient($config['host'], $config['port'], $config['clientId']);
|
|
$client->connect($settings, $config['clean']);
|
|
$client->publish($topic, $msg, $qos, $retain);
|
|
$client->disconnect();
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['ok'=>true,'topic'=>$topic,'qos'=>$qos,'retain'=>$retain]);
|