diff --git a/README.md b/README.md index 81b4794e..4cacea81 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ Get to know the preferred protocol in IoT from beginner to master. ## [MQTT Programming](https://www.emqx.com/en/blog/category/mqtt-programming) Best practice of MQTT in various clients. +- [How to Use MQTT with PHP: A Guide for Real-Time Messaging](https://www.emqx.com/en/blog/how-to-use-mqtt-in-php) ([Edit](https://github.com/emqx/blog/blob/main/en/202411/how-to-use-mqtt-in-php.md)) - [A Guide on Collecting and Reporting Soil Moisture with ESP32 and Sensor through MQTT](https://www.emqx.com/en/blog/hands-on-guide-on-esp32) ([Edit](https://github.com/emqx/blog/blob/main/en/202408/hands-on-guide-on-esp32.md)) - [MQTT on ESP32: A Beginner's Guide](https://www.emqx.com/en/blog/esp32-connects-to-the-free-public-mqtt-broker) ([Edit](https://github.com/emqx/blog/blob/main/en/202408/esp32-connects-to-the-free-public-mqtt-broker.md)) - [MQTT in Python with Paho Client: Beginner's Guide 2024](https://www.emqx.com/en/blog/how-to-use-mqtt-in-python) ([Edit](https://github.com/emqx/blog/blob/main/en/202408/how-to-use-mqtt-in-python.md)) @@ -137,7 +138,6 @@ Best practice of MQTT in various clients. - [How to Use MQTT in The Angular Project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-angular) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/how-to-use-mqtt-in-angular.md)) - [How to Use MQTT in The React Native Project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-react-native) ([Edit](https://github.com/emqx/blog/blob/main/en/202206/how-to-use-mqtt-in-react-native.md)) - [How to use MQTT in Flask](https://www.emqx.com/en/blog/how-to-use-mqtt-in-flask) ([Edit](https://github.com/emqx/blog/blob/main/en/202205/how-to-use-mqtt-in-flask.md)) -- [How to use MQTT in PHP](https://www.emqx.com/en/blog/how-to-use-mqtt-in-php) ([Edit](https://github.com/emqx/blog/blob/main/en/202108/how-to-use-mqtt-in-php.md)) - [Python MQTT Asynchronous Framework - HBMQTT](https://www.emqx.com/en/blog/python-async-mqtt-client-hbmqtt) ([Edit](https://github.com/emqx/blog/blob/main/en/202104/python-async-mqtt-client-hbmqtt.md)) - [How to use MQTT in the Electron project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-electron) ([Edit](https://github.com/emqx/blog/blob/main/en/202010/how-to-use-mqtt-in-electron.md)) - [How to use MQTT in the Vue project](https://www.emqx.com/en/blog/how-to-use-mqtt-in-vue) ([Edit](https://github.com/emqx/blog/blob/main/en/202010/how-to-use-mqtt-in-vue.md)) @@ -327,7 +327,7 @@ The easiest way to start MQTT service. Connect your IoT devices to any cloud wit - [Understanding AWS IoT Core: Features, Use Cases & Quick Tutorial](https://www.emqx.com/en/blog/understanding-aws-iot-core) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/understanding-aws-iot-core.md)) - [IoT in the Cloud: 8 Key Benefits and How to Get Started](https://www.emqx.com/en/blog/iot-in-the-cloud-8-key-benefits-and-how-to-get-started) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/iot-in-the-cloud-8-key-benefits-and-how-to-get-started.md)) - [EMQX Cloud BYOC Now Available on AWS: Seamlessly Extend Your MQTT Infrastructure](https://www.emqx.com/en/blog/emqx-cloud-byoc-is-now-available-on-aws) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/emqx-cloud-byoc-is-now-available-on-aws.md)) -- [Get Started with EMQX Cloud on Azure Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-azure-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/get-started-with-emqx-cloud-on-azure-marketplace.md)) +- [Get Started with EMQX Platform on Azure Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-azure-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202308/get-started-with-emqx-cloud-on-azure-marketplace.md)) - [Connecting to Serverless MQTT Broker using MQTTX: A Quick Guide](https://www.emqx.com/en/blog/how-to-use-mqttx-for-serverless-mqtt-broker-test) ([Edit](https://github.com/emqx/blog/blob/main/en/202307/how-to-use-mqttx-for-serverless-mqtt-broker-test.md)) - [EMQX Cloud Extends its Reach: Introducing New Regions for Seamless MQTT Connectivity](https://www.emqx.com/en/blog/emqx-cloud-extends-its-reach-introducing-new-regions-for-seamless-mqtt-connectivity) ([Edit](https://github.com/emqx/blog/blob/main/en/202307/emqx-cloud-extends-its-reach-introducing-new-regions-for-seamless-mqtt-connectivity.md)) - [EMQX Cloud BYOC is Now Available on Google Cloud!](https://www.emqx.com/en/blog/emqx-cloud-byoc-is-now-available-on-google-cloud) ([Edit](https://github.com/emqx/blog/blob/main/en/202307/emqx-cloud-byoc-is-now-available-on-google-cloud.md)) @@ -341,7 +341,7 @@ The easiest way to start MQTT service. Connect your IoT devices to any cloud wit - [Expanding Observability: Enhanced MQTT Monitor Metrics in EMQX Cloud's Latest Release](https://www.emqx.com/en/blog/enhanced-mqtt-monitor-metrics-in-emqx-cloud-s-latest-release) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/enhanced-mqtt-monitor-metrics-in-emqx-cloud-s-latest-release.md)) - [Connecting to Serverless MQTT Broker in Node.js](https://www.emqx.com/en/blog/connecting-to-serverless-mqtt-broker-in-nodejs) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/connecting-to-serverless-mqtt-broker-in-nodejs.md)) - [How BYOC MQTT Service Ensures IoT Data Security and Compliance for Your Business](https://www.emqx.com/en/blog/how-byoc-mqtt-service-ensures-iot-data-security-and-compliance-for-your-business) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/how-byoc-mqtt-service-ensures-iot-data-security-and-compliance-for-your-business.md)) -- [Get Started with EMQX Cloud on Google Cloud Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-gcp-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/get-started-with-emqx-cloud-on-gcp-marketplace.md)) +- [Get Started with EMQX Platform on Google Cloud Marketplace](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-gcp-marketplace) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/get-started-with-emqx-cloud-on-gcp-marketplace.md)) - [Optimize Your MQTT Server with EMQX Cloud BYOC's Expert O&M Service](https://www.emqx.com/en/blog/optimize-your-mqtt-server-with-emqx-cloud-byoc-s-expert-o-and-m-service) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/optimize-your-mqtt-server-with-emqx-cloud-byoc-s-expert-o-and-m-service.md)) - [Exploring the Data Privacy-First Architecture of EMQX Cloud BYOC](https://www.emqx.com/en/blog/exploring-the-data-privacy-first-architecture-of-emqx-cloud-byoc) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/exploring-the-data-privacy-first-architecture-of-emqx-cloud-byoc.md)) - [A Comprehensive Guide to Serverless MQTT Service | EMQX Cloud](https://www.emqx.com/en/blog/a-comprehensive-guide-to-serverless-mqtt-service) ([Edit](https://github.com/emqx/blog/blob/main/en/202305/a-comprehensive-guide-to-serverless-mqtt-service.md)) @@ -368,7 +368,7 @@ The easiest way to start MQTT service. Connect your IoT devices to any cloud wit - [Save MQTT Data from EMQX Cloud on GCP to Timescale Cloud through the public network ](https://www.emqx.com/en/blog/save-mqtt-data-from-emqx-cloud-on-gcp-to-timescale-cloud-through-the-public-network) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/save-mqtt-data-from-emqx-cloud-on-gcp-to-timescale-cloud-through-the-public-network.md)) - [Optimization of EMQX 5.0: The Throughput Performance of ExProto Improves | Newsletter 202209](https://www.emqx.com/en/blog/emqx-newsletter-202209) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/emqx-newsletter-202209.md)) - [EMQX Cloud Update: Comprehensive Upgrade of Billing System](https://www.emqx.com/en/blog/emqx-cloud-update-comprehensive-upgrade-of-billing-system) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/emqx-cloud-update-comprehensive-upgrade-of-billing-system.md)) -- [Get Started with EMQX Cloud on the AWS Marketplace with pay as you go](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go.md)) +- [Get Started with EMQX Platform on the AWS Marketplace with Pay As You Go](https://www.emqx.com/en/blog/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go) ([Edit](https://github.com/emqx/blog/blob/main/en/202210/get-started-with-emqx-cloud-on-the-aws-marketplace-with-pay-as-you-go.md)) - [Bridge MQTT Data from EMQX Cloud to Confluent Cloud on GCP](https://www.emqx.com/en/blog/bridge-mqtt-data-from-emqx-cloud-to-confluent-cloud-on-gcp) ([Edit](https://github.com/emqx/blog/blob/main/en/202209/bridge-mqtt-data-from-emqx-cloud-to-confluent-cloud-on-gcp.md)) - [Getting Data from EMQX Cloud with InfluxDB Native Collector](https://www.emqx.com/en/blog/getting-data-from-emqx-cloud-with-influxdb-native-collector) ([Edit](https://github.com/emqx/blog/blob/main/en/202208/getting-data-from-emqx-cloud-with-influxdb-native-collector.md)) - [EMQX Cloud update: Data integration support HStreamDB and Tablestore](https://www.emqx.com/en/blog/data-integration-support-hstreamdb-and-tablestore) ([Edit](https://github.com/emqx/blog/blob/main/en/202208/data-integration-support-hstreamdb-and-tablestore.md)) diff --git a/en/202108/how-to-use-mqtt-in-php.md b/en/202411/how-to-use-mqtt-in-php.md similarity index 58% rename from en/202108/how-to-use-mqtt-in-php.md rename to en/202411/how-to-use-mqtt-in-php.md index a443bc60..63a60308 100644 --- a/en/202108/how-to-use-mqtt-in-php.md +++ b/en/202411/how-to-use-mqtt-in-php.md @@ -1,29 +1,23 @@ -[PHP](https://www.php.net) is a widely-used open source multi-purpose scripting language, which can be embedded in HTML and is especially suitable for Web development. +[PHP](https://www.php.net/) is a widely-used open-source scripting language that is especially suitable for web development and can be embedded in HTML. It enables developers to build dynamic web applications with ease. -This article mainly introduces how to use the `php-mqtt/client` client library in PHP projects to implement the functions of connection, subscription, unsubscribing, message receiving and sending between [MQTT client](https://www.emqx.com/en/blog/mqtt-client-tools) and [MQTT broker](https://github.com/emqx/emqx). +In this guide, we’ll walk you through using the `php-mqtt/client` library to integrate **[MQTT](https://www.emqx.com/en/blog/the-easiest-guide-to-getting-started-with-mqtt)** into your PHP applications. You’ll learn how to implement essential MQTT functions such as connecting, subscribing, unsubscribing, receiving, and sending messages between the [MQTT client](https://www.emqx.com/en/blog/mqtt-client-tools) and an [MQTT broker](https://www.emqx.com/en/blog/the-ultimate-guide-to-mqtt-broker-comparison) for real-time messaging and IoT applications. +## **Choosing the Best MQTT Client Library for PHP** - -## MQTT client library selection - -This article chooses the client library `php-mqtt/client`, which has the highest downloads on composer. For more PHP-MQTT client libraries, please view in [Packagist-Search MQTT](https://packagist.org/search/?query =mqtt). - -For more documentation about php-mqtt/client, please refer to [Packagist php-mqtt/client](https://packagist.org/packages/php-mqtt/client). +For this tutorial, we'll use the **php-mqtt/client** library, which has the highest number of downloads on **Composer**. It’s a reliable, easy-to-use solution for integrating **MQTT** into PHP applications. If you’re looking for other MQTT client libraries for PHP, you can explore more options on [Packagist](https://packagist.org/search/?query=mqtt). MQTT communication belongs to a network communication scenario outside the HTTP system. Due to the limitation of PHP characteristics, using the extensions for network communication such as Swoole/Workerman in the PHP system can bring a better experience. Its use will not be repeated in this article. The relevant MQTT client libraries are as follows: - [workerman/mqtt](https://packagist.org/packages/workerman/mqtt):Asynchronous MQTT client for PHP based on workerman. - [simps/mqtt](https://packagist.org/packages/simps/mqtt):[MQTT protocol](https://www.emqx.com/en/blog/the-easiest-guide-to-getting-started-with-mqtt) Analysis and Coroutine Client for PHP. +## **Setting Up Your PHP Project for MQTT Integration** +### Confirm the PHP Version -## Project initialization - -### Confirm the PHP version - -This project uses 7.4.21 for development and testing. Readers can confirm the PHP version with the following command. +To get started, ensure that you are using PHP version 7.4.21 or higher. You can check your PHP version by running the following command in your terminal: -``` +```php php --version PHP 7.4.21 (cli) (built: Jul 12 2021 11:52:30) ( NTS ) @@ -32,49 +26,29 @@ Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies ``` -### Use Composer to install `php-mqtt/client` +### **Install the** `php-mqtt/client` **Library with Composer** -Composer is a dependency management tool for PHP, which can manage all the dependencies your PHP project needs. - -```bash -composer require php-mqtt/client -``` +Composer is a powerful dependency management tool for PHP that simplifies the installation of required libraries for your PHP projects. +To install the **php-mqtt/client** library, run the following command: +```shell +composer require php-mqtt/client +``` -## PHP MQTT usage +## PHP MQTT Usage -### Connect to the MQTT broker +### Connect to the MQTT Broker -This article will use the [free public MQTT broker](https://www.emqx.com/en/mqtt/public-mqtt5-broker) provided by EMQX, which is created on EMQX's [MQTT Cloud Service](https://www.emqx.com/en/cloud). The server access information is as follows: +This article will use the [free public MQTT broker](https://www.emqx.com/en/mqtt/public-mqtt5-broker) provided by EMQX, which is created on EMQX [MQTT Platform](https://www.emqx.com/en/cloud). The server access information is as follows: - Broker: **broker.emqx.io** - TCP Port: **1883** - SSL/TLS Port: **8883** -
-
- Open Manufacturing Hub -
-
-
- A Practical Guide to MQTT Broker Selection -
-
- Download this practical guide and learn what to consider when choosing an MQTT broker. -
- Get the eBook → -
-
- #### Import composer autoload file and `php-mqtt/client` -```php +```shell require('vendor/autoload.php'); use \PhpMqtt\Client\MqttClient; @@ -147,9 +121,9 @@ for ($i = 0; $i< 10; $i++) { $mqtt->loop(true); ``` -### Complete code +### Complete Code -Server connection, message publishing and receiving code. +Here’s the full PHP code to connect, publish, and subscribe to the MQTT broker: ```php loop(true); - ``` - ## Test After running the MQTT message publishing code, we will see that the client has successfully connected, and the messages have been published one by one and received successfully: -```bash -php pubsub_tcp.php +```shell +php pubsub_tcp.php ``` ![PHP MQTT Test](https://assets.emqx.com/images/61618d56823886f101feaf6741a20c3f.png) +## **Summary and Next Steps** -## Summary - -So far, we have used the **php-mqtt/client** to connect to the [free public MQTT broker](https://www.emqx.com/en/mqtt/public-mqtt5-broker), and implemented the connection, message publishing and subscription between the test client and the MQTT server. +In this tutorial, we demonstrated how to use the **php-mqtt/client** library in PHP to connect to an MQTT broker, publish and subscribe to [MQTT topics](https://www.emqx.com/en/blog/advanced-features-of-mqtt-topics), and receive real-time messages. This is a foundational step for building powerful, real-time applications using PHP and MQTT. -Next, you can check out [The Easy-to-understand Guide to MQTT Protocol](https://www.emqx.com/en/mqtt-guide) series of articles provided by EMQ to learn about MQTT protocol features, explore more advanced applications of MQTT, and get started with MQTT application and service development. +For more advanced MQTT tutorials, check out our [The Easy-to-understand Guide to MQTT Protocol](https://www.emqx.com/en/mqtt-guide) series to explore more advanced applications of MQTT.
- Try EMQX Cloud for Free -
No credit card required
+ Talk to an Expert
- Get Started → + Contact Us →