This topic provides a sample script for parsing data from custom topics in PHP.

Script template

You can write a data parsing script based on the following script template in Python.

<? php
/**
 * Converts custom topic data of devices to JSON data. The function is called when devices submit data to IoT Platform.
 * Input parameter: the $topic string that indicates a topic used by devices to submit data.
 * Input parameter: the $rawData array. Each element must be an integer.
 * Output parameter: the $jsonObj association array. Each key value must be a string and cannot be a numeric string such as "10". Key values cannot be left empty.
 */
function transformPayload($topic, $rawData)
{
    $jsonObj = array();
    return $jsonObj;
}

Additional considerations

  • Do not use global variables or static variables. Otherwise, results may be inconsistent.
  • In the script, data is processed 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 integer array. Use 0xFF for the bitwise AND operation to obtain the complement. The returned result is an association array. The 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 for exception handling. If an error occurs, an exception is thrown and subsequent code is not executed. To ensure code robustness, you need to capture and process exceptions.

Example

Note The following script is used only to parse custom topic data. If the Data Format of the product is Custom, you also need to write a script for parsing TSL data. For more information about parsing TSL data, see Example for parsing TSL data.
<? php
/*
  Sample data
  Customize a topic named /user/update to submit data
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;
}