├── .gitignore ├── composer.json ├── README.md ├── publish.php ├── subscribe.php └── composer.lock /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.phar 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "bluerhinos/phpmqtt": "dev-master" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP example for MQTT 2 | 3 | ## Getting started 4 | 5 | * Clone the repo 6 | * Install [Composer](https://getcomposer.org/download) if you don't already have it installed 7 | * run `php composer.phar install` 8 | * set url to CloudMQTT `export CLOUDMQTT_URL=mqtt://....` 9 | * run `php subscribe.php` 10 | * Open another terminal window 11 | * set url to CloudMQTT `export CLOUDMQTT_URL=mqtt://....` 12 | * run `php publish.php` 13 | 14 | -------------------------------------------------------------------------------- /publish.php: -------------------------------------------------------------------------------- 1 | connect(true, NULL, $url['user'], $url['pass'])) { 12 | $mqtt->publish($topic, $message, 0); 13 | echo "Published message: " . $message; 14 | $mqtt->close(); 15 | }else{ 16 | echo "Fail or time out
"; 17 | } 18 | -------------------------------------------------------------------------------- /subscribe.php: -------------------------------------------------------------------------------- 1 | connect(true, NULL, $url['user'], $url['pass'])) { 15 | $topics[$topic] = array( 16 | "qos" => 0, 17 | "function" => "procmsg" 18 | ); 19 | $mqtt->subscribe($topics,0); 20 | while($mqtt->proc()) {} 21 | $mqtt->close(); 22 | } else { 23 | exit(1); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "e7b705a945ab1c3cb8f14e7020f2499b", 8 | "packages": [ 9 | { 10 | "name": "bluerhinos/phpmqtt", 11 | "version": "dev-master", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/bluerhinos/phpMQTT.git", 15 | "reference": "d3dcdb42fa77c7ebae3e81fdd0e150805a57d8b9" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/bluerhinos/phpMQTT/zipball/d3dcdb42fa77c7ebae3e81fdd0e150805a57d8b9", 20 | "reference": "d3dcdb42fa77c7ebae3e81fdd0e150805a57d8b9", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "php": ">=5.4" 25 | }, 26 | "type": "library", 27 | "autoload": { 28 | "psr-4": { 29 | "Bluerhinos\\": "./" 30 | } 31 | }, 32 | "notification-url": "https://packagist.org/downloads/", 33 | "license": [ 34 | "BSD" 35 | ], 36 | "authors": [ 37 | { 38 | "name": "Andrew Milsted", 39 | "email": "andrew@bluerhinos.co.uk" 40 | } 41 | ], 42 | "description": "Simple MQTT Class", 43 | "time": "2017-09-06T17:57:05+00:00" 44 | } 45 | ], 46 | "packages-dev": [], 47 | "aliases": [], 48 | "minimum-stability": "stable", 49 | "stability-flags": { 50 | "bluerhinos/phpmqtt": 20 51 | }, 52 | "prefer-stable": false, 53 | "prefer-lowest": false, 54 | "platform": [], 55 | "platform-dev": [] 56 | } 57 | --------------------------------------------------------------------------------