发送qos参数和MQTT通知


sending qos parameter along with MQTT notification

我使用ponte节点应用程序从web向设备发送MQTT通知,请参阅下面我使用的格式。

"http://www.example.com/resources/topic/"和消息作为正文,从邮递员测试。我的问题是我如何发送qos和保留参数,同时使用PHP CURL发送MQTT通知?我的PHP代码如下:

$curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_PORT              => self::MQTT_SERVER_PORT,
              CURLOPT_URL               => self::MQTT_SERVER_URL.'/'.$topic,
              CURLOPT_RETURNTRANSFER    => true,
              CURLOPT_ENCODING          => "",
              CURLOPT_MAXREDIRS         => 10,
              CURLOPT_TIMEOUT           => 30,
              CURLOPT_HTTP_VERSION      => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST     => "PUT",
              CURLOPT_POSTFIELDS        => "test message"
            ));
            curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            if(!$err){
                $sent = true;
            }

问候,Tismon Varghese .

如果您想发送MQTT消息,则不可能使用PHP curl,因为它使用HTTP协议(以及其他一些协议,但不是MQTT)发送数据。

MQTT是轻量级协议,它取代了HTTP来发送消息。您可以使用以下代码用PHP发送MQTT消息: https://github.com/bluerhinos/phpMQTT

下面是向MQTT代理发布数据的示例代码(带有qos和retain参数):

require("phpMQTT.php");
$brokder = "address";  
$port = 1883;
$clientName = "Client00";  
$topic = "topic"; 
$message = "Test Message";
$qos = 0;
$retain = 0;
$mqtt = new phpMQTT($broker, $portNo, $clientName);
if ($mqtt->connect()) {
$mqtt->publish($topic, $message, $qos, $retain);
$mqtt->close();
} else {
echo "Error Occured";
}

CURL(现在为7.76.1)不支持高于0的QoS