Can't connect using phpMQTT.php

Hello, i’m sending data from an ESP32 to HiveMQ cloud and it works perfectly. Now i need to get those data and put them in a sql database connected to my web app.

I’m trying to do that using a php library i found called phpMQTT.php, but i can’t connect to the broker.

I tried ports 8883 and 1883 but don’t work.

Here is the code i’m using:

<?php require('phpMQTT.php'); $server = 'xxx.s2.eu.hivemq.cloud'; // change if necessary $port = 8883; // change if necessary $username = 'xxx'; // set your username $password = 'xxx'; // set your password $client_id = uniqid('mqtt_client_'); $mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id); if ($mqtt->connect(true, NULL, $username, $password)) { $mqtt->publish('esp32/connection_count', 'Hello World! at ' . date('r'), 0, false); $mqtt->close(); } else { echo "Time out!\n"; } Thanks

Hello @akkyuz

Welcome to HiveMQ Community! What is the error message being dropped while you are executing this PHP code? Could you please share more details?

Kind regards,
Diego from HiveMQ Team

Hello @Diego ,
it prints “Time out!”, that looking at the code means that could not connect. But the credentials should be correct

Hi @akkyuz looking into our community forum I saw this thread Cannot connect using phpMQTT

It seems that PHP library does require our server cert (CA).

You can download the root certificate here .
This will create a file called “isrgrootx1.pem”, which you can use as a “Server Certificate”.

Kind regards,
Diego from HiveMQ Team

Hi @akkyuz

Check out my code sample below of how I put this phpMQTT library to work with HiveMQ Cloud.

Example: publish.php

<?php

require('../phpMQTT.php');

$server = 'TYPE_YOUR_HIVEMQ_CLUSTER_URL';     // change if necessary
$port = 8883;                     // change if necessary
$username = 'TYPE_YOUR_USERNAME';                   // set your username
$password = 'TYPE_YOUR_PASSWORD';                   // set your password
$client_id = 'phpMQTT-publisher'; // make sure this is unique for connecting to sever - you could use uniqid()
$cafile = 'isrgrootx1.pem'; // HiveMQ Cloud CA

$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id, $cafile);

if ($mqtt->connect(true, NULL, $username, $password)) {
	$mqtt->publish('bluerhinos/phpMQTT/examples/publishtest', 'Hello World! at ' . date('r'), 0, false);
	$mqtt->close();
} else {
    echo "Time out!\n";
}

Kind regards,
Diego from HiveMQ Team