topic description
there is always an error that MosquittoClient cannot find when using Mosquitto-PHP, in laravel
sources of topics and their own ideas
I have compiled mosquitto.so, and added php.ini, and have no problem using the following code:
<?php
$c = new Mosquitto\Client;
$c->onConnect(function() use ($c) {
$c->publish("mgdm/test", "Hello", 2);
$c->disconnect();
});
$c->connect("test.mosquitto.org");
// Loop around to permit the library to do its work
// This function will call the callback defined in `onConnect()`
// and disconnect cleanly when the message has been sent
$c->loopForever();
echo "Finished\n";
because the laravel framework is used in the project, after I put the above code in it, there is always an error of SymfonyComponentDebugExceptionFatalThrowableError: Class" MosquittoClient" not found.
The code forlaravel is as follows:
<?php
namespace App\Services;
use Illuminate\Support\Facades\Cache;
use Log;
use Mosquitto\Client;
class QueueService
{
public function __construct(){
}
public function send($payload, $cb){
if(!is_callable($cb)){
$cb = function(){};
}
if(!$payload){
$cb([
"status" => true,
"code" => "",
"data" => ["c" => $c, "send" => $send],
"message" => ""
]);
return;
}
$broker = env("AMQP_ACTIVEMQ_BROKER", "");
$port = env("AMQP_ACTIVEMQ_PORT", "");
$topic = env("AMQP_ACTIVEMQ_TOPIC", "");
if(!$broker || !$topic || !$port){
$cb([
"status" => true,
"code" => "",
"data" => [],
"message" => ": Broker Topic "
]);
return;
}
try{
$url = $broker.":".$port;
//$c = new \Mosquitto\Client;
$c = new Client;
$c->onConnect(function($rc, $message) use ($c, $topic, $payload) {
$c->publish($topic, $payload, 2);
});
$c->onPublish(function($mid) use($c){
$cb([
"status" => true,
"code" => "",
"data" => [
"mid" => $mid
],
"message" => ""
]);
$c->disconnect();
});
$c->connect($url);
$c->loopForever();
} catch (\Exception $e) {
$cb([
"status" => false,
"code" => "",
"data" => [],
"message" => ": ".$e->getMessage()
]);
return;
}
}
}
does anyone know how to use the MosquittoClient class in laravel?