1

I am using ponte node application for sending MQTT notification from web to devices and please see below the format I am using.

"http://www.example.com/resources/topic/" and message as body while testing from postman. My question is how can i send qos and retain parameter while sendin MQTT notification using PHP CURL ? My PHP code is below.

$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;
            }

Regards, Tismon Varghese.

hardillb
  • 54,545
  • 11
  • 67
  • 105
Tismon Varghese
  • 361
  • 1
  • 7
  • 18

2 Answers2

-1

If you want to send MQTT messages, it is not possible to use PHP curl as it sends data using HTTP protocol (and some others but not MQTT).

MQTT is lightweight protocol that replaces HTTP for sending messages. You can use the following code for sending MQTT messages with PHP:

https://github.com/bluerhinos/phpMQTT

Here's a sample code for publishing data to MQTT broker (with qos and retain parameters):

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";

}
Mehdi
  • 186
  • 2
  • 8
  • Thanks for the answer. But my code works perfectly. Ponte (http://www.eclipse.org/ponte/) is an application which supports both HTTP and MQTT. Since it is already implemented; I am looking for a method that can send `QoS` parameter along with message. – Tismon Varghese Nov 15 '16 at 08:03
  • I didn't know about Ponte. It is interesting. Thanks for telling me about it. – Mehdi Nov 15 '16 at 17:22
-1

CURL(now 7.76.1) does not support higher QoS then 0.

Ernestas Stankevičius
  • 2,420
  • 2
  • 24
  • 30
  • It's really not clear what relevance this has to the question asked. Curl is being used to make a HTTP request to the backend which then sends a MQTT message – hardillb May 05 '21 at 17:43
  • @hardillb "My question is how can i send qos and retain parameter while sendin MQTT notification using PHP CURL?" CURL supports: DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, **MQTT**, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, HTTP/3. And he ask how he can retain QoS. He can not, because it supports only QoS-0. – Ernestas Stankevičius May 07 '21 at 07:25
  • 1
    Re-read the question, it is making a HTTP request not a MQTT request. it explicitly has a HTTP verb `PUT` and uses `CURL_HTTP_VERSION_1_1,` to the Ponte project (a now archived Eclipse Project) which was a HTTP to MQTT bridge. – hardillb May 07 '21 at 07:36