# Can't connect using phpMQTT.php

**URL:** <https://community.hivemq.com/t/cant-connect-using-phpmqtt-php/2046>\
**Category:** HiveMQ Cloud\
**Created:** [May 5, 2023, 10:11am UTC](https://community.hivemq.com/t/cant-connect-using-phpmqtt-php/2046 "2023-05-05T10:11:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![akkyuz](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@akkyuz](https://community.hivemq.com/u/akkyuz)\
**Post date:** [May 5, 2023, 10:11am UTC](https://community.hivemq.com/t/cant-connect-using-phpmqtt-php/2046/1 "2023-05-05T10:11:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://community.hivemq.com/u/Diego)\
**Post date:** [May 5, 2023, 12:04pm UTC](https://community.hivemq.com/t/cant-connect-using-phpmqtt-php/2046/2 "2023-05-05T12:04:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akkyuz](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@akkyuz](https://community.hivemq.com/u/akkyuz)\
**Post date:** [May 5, 2023, 12:33pm UTC](https://community.hivemq.com/t/cant-connect-using-phpmqtt-php/2046/3 "2023-05-05T12:33:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://community.hivemq.com/u/Diego)\
**Post date:** [May 5, 2023, 1:36pm UTC](https://community.hivemq.com/t/cant-connect-using-phpmqtt-php/2046/4 "2023-05-05T13:36:53Z")

</div>

Hi @akkyuz looking into our community forum I saw this thread [Cannot connect using phpMQTT](https://community.hivemq.com/t/cannot-connect-using-phpmqtt/1593)

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

You can download the root certificate [here](https://letsencrypt.org/certs/isrgrootx1.pem).  
This will create a file called “isrgrootx1.pem”, which you can use as a “Server Certificate”.

Kind regards,  
Diego from HiveMQ Team

---

<div class="post-metadata">

**Author:** ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://community.hivemq.com/u/Diego)\
**Post date:** [May 5, 2023, 4:42pm UTC](https://community.hivemq.com/t/cant-connect-using-phpmqtt-php/2046/5 "2023-05-05T16:42:32Z")

</div>

Hi @akkyuz

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

Example: publish.php

```auto
<?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";
}

```

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hivemq1/original/1X/667f2b3e5938ab51f8d28d3fdf1461daca475776.png)

Kind regards,  
Diego from HiveMQ Team
