PHP中的Rabbit MQTT客户端?

时间:2017-09-07 09:38:40

标签: phpmqtt

我是MQTT的新手。任何人都可以帮助如何在PHP中使用Rabbitmq mqtt,我在云中有MQTT代理,所以我想在我的本地系统中基于PHP进行开发。我们要下载的任何图书馆?任何人都可以在Ubuntu帮忙吗?

1 个答案:

答案 0 :(得分:0)

您可以使用客户端lib:https://github.com/bluerhinos/phpMQTT,如下所述:https://www.cloudamqp.com/docs/php_mqtt.html

出版商

require("phpMQTT.php");
$host = "hostname";
$port = port;
$username = "username";
$password = "password";
$message = "Hello CloudAMQP MQTT!";

//MQTT client id to use for the device. "" will generate a client id     automatically
$mqtt = new 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");

$host = "hostname";
$port = port;
$username = "username";
$password = "password";

$mqtt = new 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";
}   

默认情况下,在所有CloudAMQP服务器上启用MQTT,因此如果您将CloudAMQP用作MQTT代理,则无需启用MQTT插件。如果没有,则需要启用此插件:https://www.rabbitmq.com/mqtt.html