ファイナンス、情報通信技術のスキル・アグリゲーション・サイト

' . iseeit.jp 情報通信技術 . '
 

PHP で MQTT クライアント(php-mqtt/client)

PHP で MQTT クライアントを作成します。ブローカーには、Mosquitto を使用していますので、php-mqtt/client ライブラリを利用しました。

PHP で MQTT クライアント(Mosquitto-PHP)」で使用している Mosquitto-PHP ライブラリは、PHP 8.3 まではインストール可能でしたが、それ以降のバージョン PHP 8.4、PHP 8.5 ではインストールに失敗します。そこで、php-mqtt/client ライブラリに移行しました。

まず、PHP の依存管理ツールである Composer のインストール方法と、MQTT クライアントライブラリ php-mqtt/client の導入方法、さらに簡単な使用例を紹介します。

さらに、php-mqtt/client を使った以下の応用例を示します。

  • QoS レベルを指定した publish / subscribe
  • ユーザー名・パスワードによる認証
  • TLS(SSL)を使った暗号化通信

1. Composer のインストール

Composer は PHP の標準的なパッケージ管理ツールです。以下の手順でインストールできます。

1-1. インストーラのダウンロード

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

1-2. インストール実行

php composer-setup.php

1-3. composer.phar を /usr/local/bin に移動(任意)

sudo mv composer.phar /usr/local/bin/composer

これで composer コマンドがシステム全体で利用できます。

2. php-mqtt/client のインストール

Composer を使って MQTT クライアントライブラリをインストールします。

2-1. プロジェクトディレクトリを作成

mkdir mqtt-test
cd mqtt-test

2-2. php-mqtt/client をインストール

composer require php-mqtt/client

これで vendor/ ディレクトリにライブラリが追加され、 autoload.php を読み込むことで利用できるようになります。

3. php-mqtt/client の簡単な使用例

以下は、MQTT ブローカーに接続してメッセージを送信する最小構成の例です。

3-1. publish(メッセージ送信)例

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'localhost';   // MQTT ブローカーのホスト
$port     = 1883;          // ポート
$clientId = 'php-mqtt-publisher';

$connectionSettings = (new ConnectionSettings)
    ->setUsername(null)
    ->setPassword(null)
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

$client->publish('test/topic', 'Hello MQTT from PHP!', 0);

$client->disconnect();

3-2. subscribe(メッセージ受信)例

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'localhost';
$port     = 1883;
$clientId = 'php-mqtt-subscriber';

$connectionSettings = (new ConnectionSettings)
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

$client->subscribe('test/topic', function ($topic, $message) {
    echo "Received on {$topic}: {$message}\n";
}, 0);

$client->loop(true);   // 無限ループで待機

これで PHP から MQTT ブローカーに接続し、 メッセージの送受信ができるようになります。

4. QoS を指定した publish / subscribe

QoS(Quality of Service)は MQTT の配信品質レベルです。代表的には以下の 3 つがあります。

  • QoS 0: at most once(届くかもしれない)
  • QoS 1: at least once(1回以上必ず届くが重複の可能性あり)
  • QoS 2: exactly once(重複なしで必ず1回だけ届く)

4-1. QoS 1 で publish する例

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'localhost';
$port     = 1883;
$clientId = 'php-mqtt-publisher-qos1';

$connectionSettings = (new ConnectionSettings)
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

// QoS 1 で publish
$topic   = 'demo/qos';
$message = 'Message with QoS 1';

$client->publish($topic, $message, 1, false);

$client->disconnect();

4-2. QoS 2 で subscribe する例

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'localhost';
$port     = 1883;
$clientId = 'php-mqtt-subscriber-qos2';

$connectionSettings = (new ConnectionSettings)
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

// QoS 2 で subscribe
$client->subscribe('demo/qos', function (string $topic, string $message) {
    echo "Received on {$topic} (QoS2): {$message}\n";
}, 2);

$client->loop(true);

5. ユーザー名・パスワードによる認証

MQTT ブローカー側でユーザー認証が有効な場合、ConnectionSettings に ユーザー名とパスワードを設定します。

5-1. 認証付き接続 + publish の例

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'mqtt.example.com';
$port     = 1883;
$clientId = 'php-mqtt-auth-publisher';

$username = 'myuser';
$password = 'mypassword';

$connectionSettings = (new ConnectionSettings)
    ->setUsername($username)
    ->setPassword($password)
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

$client->publish('secure/topic', 'Authenticated message', 1, false);

$client->disconnect();

5-2. 認証付き接続 + subscribe の例

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'mqtt.example.com';
$port     = 1883;
$clientId = 'php-mqtt-auth-subscriber';

$username = 'myuser';
$password = 'mypassword';

$connectionSettings = (new ConnectionSettings)
    ->setUsername($username)
    ->setPassword($password)
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

$client->subscribe('secure/topic', function (string $topic, string $message) {
    echo "Received (auth): {$topic} => {$message}\n";
}, 1);

$client->loop(true);

6. TLS(SSL)対応の接続

TLS を使うことで、MQTT 通信を暗号化できます。多くのブローカーでは ポート 8883 が TLS 用に使われます。

6-1. サーバ証明書のみを使った TLS 接続

ブローカーが正式な CA で署名された証明書を使っている場合の、最小構成の例です。

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'mqtts.example.com'; // TLS 対応ブローカー
$port     = 8883;                // TLS ポート
$clientId = 'php-mqtt-tls-client';

$connectionSettings = (new ConnectionSettings)
    ->setUseTls(true)            // TLS を有効化
    ->setTlsVerifyPeer(true)     // 証明書の検証を有効
    ->setTlsVerifyPeerName(true) // ホスト名検証を有効
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

$client->publish('tls/topic', 'Hello over TLS', 1, false);

$client->disconnect();

6-2. 自己署名証明書を使う場合の TLS 接続

自己署名証明書を使う場合は、検証方法を緩めたり、独自 CA を指定します。

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'mqtts.local';
$port     = 8883;
$clientId = 'php-mqtt-tls-selfsigned';

$connectionSettings = (new ConnectionSettings)
    ->setUseTls(true)
    // 自己署名証明書を許可
    ->setTlsSelfSignedAllowed(true)
    // 必要に応じて CA ファイルやパスを指定
    ->setTlsCertificateAuthorityFile('/path/to/ca.crt')
    // ホスト名検証を無効にする場合(開発用途など)
    ->setTlsVerifyPeerName(false)
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

$client->subscribe('tls/selfsigned', function (string $topic, string $message) {
    echo "TLS (self-signed) {$topic}: {$message}\n";
}, 1);

$client->loop(true);

6-3. クライアント証明書付き TLS(双方向認証)の例

ブローカーがクライアント証明書による認証を要求する場合は、 クライアント証明書と秘密鍵を設定します。

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\ConnectionSettings;

$server   = 'mqtts.example.com';
$port     = 8883;
$clientId = 'php-mqtt-tls-mutual';

$connectionSettings = (new ConnectionSettings)
    ->setUseTls(true)
    ->setTlsVerifyPeer(true)
    ->setTlsVerifyPeerName(true)
    ->setTlsCertificateAuthorityFile('/path/to/ca.crt')
    // クライアント証明書と秘密鍵
    ->setTlsClientCertificateFile('/path/to/client.crt')
    ->setTlsClientCertificateKeyFile('/path/to/client.key')
    // 鍵にパスフレーズがある場合
    // ->setTlsClientCertificateKeyPassphrase('secret-passphrase')
    ->setKeepAliveInterval(60);

$client = new MqttClient($server, $port, $clientId);

$client->connect($connectionSettings, true);

$client->publish('tls/mutual', 'Mutual TLS message', 1, false);

$client->disconnect();

ファイナンシャル・プランニング
6つの係数

終価係数 : 元本を一定期間一定利率で複利運用したとき、将来いくら になるかを計算するときに利用します。

現価係数 : 将来の一定期間後に目標のお金を得るために、現在いくら の元本で複利運用を開始すればよいかを計算するときに利用します。

年金終価係数 : 一定期間一定利率で毎年一定金額を複利運用で 積み立て たとき、将来いくら になるかを計算するときに利用します。

年金現価係数 : 元本を一定利率で複利運用しながら、毎年一定金額を一定期間 取り崩し ていくとき、現在いくら の元本で複利運用を開始すればよいかを計算するときに利用します。

減債基金係数 : 将来の一定期間後に目標のお金を得るために、一定利率で一定金額を複利運用で 積み立て るとき、毎年いくら ずつ積み立てればよいかを計算するときに利用します。

資本回収係数 : 元本を一定利率で複利運用しながら、毎年一定金額を一定期間 取り崩し ていくとき、毎年いくら ずつ受け取りができるかを計算するときに利用します。

積み立て&取り崩しモデルプラン

積立金額→年金額の計算 : 年金終価係数、終価係数、資本回収係数を利用して、複利運用で積み立てた資金から、将来取り崩すことのできる年金額を計算します。

年金額→積立金額の計算 : 年金現価係数、現価係数、減債基金係数を利用して、複利運用で将来の年金プランに必要な資金の積立金額を計算します。


住宅ローン計算ツール