1. Go to this page and download the library: Download gmajor/substrate-codec-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gmajor / substrate-codec-php example snippets
ubstrate\ScaleCodec\Types\TypeRegistry;
use Substrate\ScaleCodec\Bytes\ScaleBytes;
// Create type registry
$registry = new TypeRegistry();
// Encode a U32 value
$u32 = $registry->get('U32');
$encoded = $u32->encode(12345);
echo $encoded->toHex(); // 0x39300000
// Decode it back
$bytes = ScaleBytes::fromHex('0x39300000');
$decoded = $u32->decode($bytes);
echo $decoded; // 12345
$compact = $registry->get('Compact');
// Small values (0-63): 1 byte
echo $compact->encode(42)->toHex(); // 0xa8
// Medium values (64-16383): 2 bytes
echo $compact->encode(100)->toHex(); // 0x9101
// Large values
echo $compact->encode(1000000)->toHex(); // 0x02c0843d00
$optionU32 = (new OptionType($registry))->setInnerType($registry->get('U32'));
// Some value
$encoded = $optionU32->encode(42); // 0x012a000000
// None value
$encoded = $optionU32->encode(null); // 0x00