Search code examples
phpapachemosquittolibmosquitto

Mosquitto PHP runs on Terminal but does not run in Browser Error 500


I'm facing a problem developing a Mosquitto client in php. When I execute my test code through the Terminal on Ubuntu the code works very well like this:

root@ip-*********:/var/www/html/pairing/mqtt# php pub.php Mesage published Disconnected cleanly root@ip-***********:/var/www/html/pairing/mqtt#

But when I run this the same code on Browser every time I get the HTTP ERROR 500.

Anyone know what can be this problem? I just ajust the permissions through chmod but it does not work yet.

Here is the code:

<?php

$client = new Mosquitto\Client(); $client->onConnect('connect'); $client->onDisconnect('disconnect'); $client->onPublish('publish'); $client->connect("test.mosquitto.org", 1883, 5);

while (true) {
        try{
                $client->loop();
                $mid = $client->publish('/mqtt', "Hello from PHP");
                $client->loop();
        }catch(Mosquitto\Exception $e){
                return;
        }
        sleep(2); }

$client->disconnect(); unset($client);

function connect($r) {
        echo "I got code {$r}\n"; }

function publish() {
        global $client;
        echo "Mesage published\n";
        $client->disconnect(); }

function disconnect() {
        echo "Disconnected cleanly\n"; }

?>

Thanks a lot!


Solution

  • I'm assuming you are using Apache as web server, if so check whether mosquitto module is enabled using else properly set enable it in /etc/php5/apache2/php.ini