This article provides a template and a sample script in PHP for parsing data that is sent to custom topics.

Script template

You can write a data parsing script in PHP based on the following script template:

<? php
/**
 * Call a function to convert the data that is sent by a device to a custom topic into JSON-formatted data when the device reports data to IoT Platform.
 * $topic: an input parameter that specifies the topic to which the device sends messages. The value is a string.
 * $rawData: an input parameter. The value is an array of integers.
 * $jsonObj: an output parameter. The value is a JSON object that indicates an association array. The value of each key must be a string that is not a numeric string such as "10". Key values cannot be empty.
 */
function transformPayload($topic, $rawData)
{
    $jsonObj = array();
    return $jsonObj;
}

Notes for script writing

  • Do not use global variables or static variables. Otherwise, results may be inconsistent.
  • In the script, process data by using the two's complement operation. The complement range for values in the range of [-128,127] is [0,255]. For example, the complement of -1 is 255 in decimal.
  • The input parameter of the transformPayload function is an array of integers. Use 0xFF for the bitwise AND operation to obtain the complement. The return result is an association array. Each key value must contain non-array characters. For example, if a key value is "10", the PHP array retrieves the integer 10.
  • The PHP runtime environment is significantly strict with exception handling. If an error occurs, an exception is thrown and subsequent code is not run. To ensure code robustness, you need to capture and process exceptions.

Sample script

Note The following sample script can be used only to parse data that is sent to custom topics. If the Data Type parameter of the product is set to Custom, you also need to write a script for parsing Thing Specification Language (TSL) data. For information about how to write a script for parsing TSL data, see Example for parsing TSL data.
<? php
/*
  Sample data
  Custom topic:
     /user/update, to which data is sent.
  Input parameters:
     topic: /{productKey}/{deviceName}/user/update
     bytes: 0x000000000100320100000000
  Output parameters:
  {
     "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;
}