This topic provides a PHP script template and a sample PHP script that you can use to parse data that is sent to custom topics.

Script template

You can write a PHP script to parse data based on the following PHP template:

<?php
/**
 * Convert the data that is sent by a device to a custom topic to JSON data when the device submits data to IoT Platform. 
 * $topic: the input parameter that specifies the topic to which the device sends messages. The value is a string. 
 * $rawData: the 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 messages that are sent to custom topics. If you set the Data Type parameter of the product to Custom, you must also write a script to parse Thing Specification Language (TSL) messages. For information about how to write a script to parse TSL messages, see Submit a script to parse TSL data.

For information about custom data formats, see Create a product.

<?php
/*
  Sample data
  Use the following custom topic to submit data:
     /user/update. 
  Input:
     topic: /${productKey}/${deviceName}/user/update
     bytes: 0x000000000100320100000000 
  Output:
  {
     "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;
}