karpy47/PhpMqttClient

Newbie here ...

Closed this issue · 1 comments

Hello!

I never used Composer before. I struggled with it, and managed to get composer.phar (showing options by running php composer.phar) inside my linux user folder, where /public_html is also in...

I installed your package. A /vendor folder appeared, etc. (but nothing inside /public_html).

I did navigate into my public_html folder and created a test.php. At top I placed a:

require_once '../vendor/autoload.php';

At first run, it did NOT give an error. As was my initial understanding, composer's autoload.php should include all installed packages so I can use their classes (including yours). but NOPE:

I proceeded to copy your demo code, changed connection info into my own mqtt server, port, user,pwd,client id, etc. and rerun the test.php file only to get the following error:

Fatal error: Uncaught Error: Class 'MQTTClient' not found in /home/<user>/domains/<subdomain>/public_html/test.php:2 Stack trace: #0 {main} thrown in /home/<user>/domains/<subdomain>/public_html/test.php on line 2

Then at top of my test.php, I changed the required once, now referencing your file, directly with:

require_once '../vendor/karpy47/php-mqtt-client/MQTTClient.php';

and now my PHP is giving the following error:

Parse error: syntax error, unexpected '/', expecting '{' in /home/<user>/domains/<subdomain>/vendor/karpy47/php-mqtt-client/MQTTClient.php on line 6

So now, I get into your code in MQTTClient.php and comment out line 6:

//namespace karpy47/php-mqtt-client;

AND FINALLY I GOT IT TO WORK!.

IN SHORT, my questions:

  1. I am using composer in the right way ??

  2. Isn't it easier to just grab your MQTTClient.php, paste it into /vendors or /includes or whatever folder and reference it ... and dump all the "composer" package installer / maintainer ?

Please any help and comments are appreciated, since I am very aware I might be doing something wrong and potentially unsecure.

Enrique

Hi Enrique,

I too remember setting up composer the first time was a pain.

If you only plan to us a few external packages and do not want to have the code updated (fixes, etc) automatically, you actually do not need to use Composer and may save some worries. However, as soon as your project grows it is an invaluable tool for adding good code and keeping it updated with new releases from whomever is responsible for that code. I've used it extensively myself.

Read carefully about basic usage and you should get it working. Make sure require_once '../vendor/autoload.php'; is pointing to the right directory (change to require __DIR__ . '/vendor/autoload.php'; and/or debug that path).

After that you need to add a use-clause like this to include the code in the scope of your code:

use karpy47\PhpMqttClient\MQTTClient

Now the example might work better.
Good luck