A simple php class that can be used to connect, publish and subscribe from CloudAMQP MQTT is phpMQTT.
The ClientId is the unique MQTT client id to use for the device.
Download phpMQTT.php from the link above and place the file in the same folder as the following files.
//config.php, you can all these on the details page for your instance
$host = "***.cloudmqtt.com";
$port = ;
$username = "";
$password = "";
require("phpMQTT.php");
require("config.php");
$message = "Hello CloudAMQP MQTT!";
//MQTT client id to use for the device. "" will generate a client id automatically
$mqtt = new bluerhinos\phpMQTT($host, $port, "ClientID".rand());
if ($mqtt->connect(true,NULL,$username,$password)) {
$mqtt->publish("topic",$message, 0);
$mqtt->close();
}else{
echo "Fail or time out
";
}
require("phpMQTT.php");
require("config.php");
$mqtt = new bluerhinos\phpMQTT($host, $port, "ClientID".rand());
if(!$mqtt->connect(true,NULL,$username,$password)){
exit(1);
}
//currently subscribed topics
$topics['topic'] = array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($topics,0);
while($mqtt->proc()){
}
$mqtt->close();
function procmsg($topic,$msg){
echo "Msg Recieved: $msg";
}
First, start the subscriber with `php subscriber.php` and then in another terminal window start the publisher `php publisher.php` and you should see your message printed in the first terminal window.
Another option is to use Mosquitto-PHP. Good examples can be found here.