すべてのプロダクト
Search
ドキュメントセンター

IoT Platform:PHP サンプルスクリプト

最終更新日:Mar 26, 2025

このトピックでは、カスタムトピックに送信されたデータを解析するために使用できる PHP スクリプトテンプレートと PHP サンプルスクリプトについて説明します。

スクリプトテンプレート

次の PHP テンプレートに基づいて、データを解析する PHP スクリプトを作成できます。

<?php
/**
 * デバイスが IoT Platform にデータを送信するときに、デバイスによってカスタムトピックに送信されたデータを JSON データに変換します。
 * $topic: デバイスがメッセージを送信するトピックを指定する入力パラメーター。値は文字列です。
 * $rawData: 入力パラメーター。値は整数の配列です。
 * $jsonObj: 出力パラメーター。値は、連想配列を示す JSON オブジェクトです。各キーの値は、「10」などの数値文字列ではない文字列である必要があります。キー値を空にすることはできません。
 */
function transformPayload($topic, $rawData)
{
    $jsonObj = array();
    return $jsonObj;
}
?>

スクリプト作成の注意事項

  • グローバル変数または静的変数は使用しないでください。そうしないと、結果に一貫性がなくなる可能性があります。
  • スクリプトでは、2 の補数演算を使用してデータを処理します。[-128,127] の範囲の値の補数の範囲は [0,255] です。たとえば、-1 の補数は 10 進数で 255 です。
  • transformPayload 関数の入力パラメーターは整数の配列です。ビット単位の AND 演算に 0xFF を使用して、補数を取得します。戻り値は連想配列です。各 キー 値には、配列以外の文字が含まれている必要があります。たとえば、キー 値が「10」の場合、PHP 配列は整数 10 を取得します。
  • PHP ランタイム環境は、例外処理に関して非常に厳格です。エラーが発生すると、例外がスローされ、後続のコードは実行されません。コードの堅牢性を確保するために、例外を捕捉して処理する必要があります。

サンプルスクリプト

説明 次のサンプルスクリプトは、カスタムトピックに送信されたメッセージを解析するためにのみ使用できます。[データ型] パラメーターを [カスタム] に設定した場合は、Thing Specification Language(TSL)メッセージを解析するスクリプトも作成する必要があります。TSL メッセージを解析するスクリプトの作成方法については、「TSL データを解析するスクリプトを送信する」をご参照ください。

[カスタムデータ形式] については、「プロダクトを作成する」をご参照ください。

<?php
/*
  サンプルデータ
  次のカスタムトピックを使用してデータを送信します。
     /user/update. 
  入力:
     topic: /${productKey}/${deviceName}/user/update
     bytes: 0x000000000100320100000000 
  出力:
  {
     "prop_float": 0,
     "prop_int16": 50,
     "prop_bool": 1,
     "topic": "/${productKey}/${deviceName}/user/update"
   }
 */
function transformPayload($topic, $bytes)
{
    $data = array();
    $length = count($bytes);
    for ($i = 0; $i < $length; $i++) {
        $data[$i] = $bytes[$i] & 0xff;
    }

    $jsonMap = array();

    if (strpos($topic, '/user/update/error') !== false) {
        $jsonMap['topic'] = $topic;
        $jsonMap['errorCode'] = getInt8($data, 0);
    } else if (strpos($topic, '/user/update') !== false) {
        $jsonMap['topic'] = $topic;
        $jsonMap['prop_int16'] = getInt16($data, 5);
        $jsonMap['prop_bool'] = $data[7];
    }

    return $jsonMap;
}

function getInt32($bytes, $index)
{
    $array = array($bytes[$index], $bytes[$index + 1], $bytes[$index + 2], $bytes[$index + 3]);

    return hexdec(byteArrayToHexString($array));
}

function getInt16($bytes, $index)
{
    $array = array($bytes[$index], $bytes[$index + 1]);

    return hexdec(byteArrayToHexString($array));
}

function getInt8($bytes, $index)
{
    $array = array($bytes[$index]);
    return hexdec(byteArrayToHexString($array));
}

function byteArrayToHexString($data)
{
    $hexStr = '';
    for ($i = 0; $i < count($data); $i++) {
        $hexValue = dechex($data[$i]);

        $tempHexStr = strval($hexValue);

        if (strlen($tempHexStr) === 1) {
            $hexStr = $hexStr . '0' . $tempHexStr;
        } else {
            $hexStr = $hexStr . $tempHexStr;
        }
    }

    return $hexStr;
}

function hexStringToByteArray($hex)
{
    $result = array();
    $index = 0;
    for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
        $result[$index++] = hexdec($hex[$i] . $hex[$i + 1]);
    }
    return $result;
}


function concat($array, $data)
{
    return array_merge($array, $data);
}

function toHex($data)
{
    $var = dechex($data);
    $length = strlen($var);
    if ($length % 2 == 1) {
        $var = '0' . $var;
    }
    return $var;
}