Tüm Ürünler
Search
Belge Merkezi

IoT Platform:PHP örnek betiği

Son Güncelleme:Oct 03, 2026

Bu konu, özel konulara gönderilen verileri ayrıştırmak için kullanabileceğiniz bir PHP betik şablonu ve örnek bir PHP betiği sunar.

Betik şablonu

Aşağıdaki PHP şablonunu temel alarak verileri ayrıştıran bir PHP betiği yazabilirsiniz:

<?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;
}
?>

Betik yazma hakkında notlar

  • Genel veya statik değişkenler kullanmayın. Aksi takdirde sonuçlar tutarsız olabilir.

  • Betiğin içinde verileri iki tümleyen işlemiyle işleyin. [-128.127] aralığındaki değerlerin tümleyen aralığı [0.255]'tir. Örneğin, -1'in ondalık tabandaki tümleyeni 255'tir.

  • transformPayload işlevinin giriş parametresi bir tamsayı dizisidir. Bit düzeyinde VE işlemi uygulayarak tümleyeni elde etmek için 0xFF değerini kullanın. Dönüş sonucu bir ilişkilendirilmiş dizidir. Her bir key değeri dizi olmayan karakterlerden oluşmalıdır. Örneğin, bir key değeri "10" ise PHP dizisi 10 tamsayısını döndürür.

  • PHP çalışma zamanı ortamı, özel durum yönetimi konusunda oldukça katıdır. Bir hata oluştuğunda bir özel durum tetiklenir ve sonraki kod çalıştırılmaz. Kod dayanıklılığını sağlamak amacıyla özel durumları yakalayarak işlemeniz gerekir.

Örnek betik

Not

Aşağıdaki örnek betik yalnızca özel konulara gönderilen mesajları ayrıştırır. Ürünün

Data Type

parametresini

Custom

olarak ayarlarsanız Thing Specification Language (TSL) mesajlarını ayrıştırmak için de bir betik yazmanız gerekir. Daha fazla bilgi için bkz.

Submit a TSL model message parsing script

.

custom data formats hakkında bilgi için bkz. 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;
}