2tvenom/cborencode

PHP 的 CBOR 编码器

1.0.2 2020-10-27 07:22 UTC

This package is auto-updated.

Last update: 2024-08-29 04:08:59 UTC


README

从 PHP 数据到 CBOR 二进制字符串的解码器/编码器。此代码自 2014 年 1 月由 Ven 开发和维护。

CBOR 是由 IETF(互联网工程任务组)定义的对象表示格式。该规范最近已被批准为 IETF 标准跟踪规范,并已作为 RFC 7049 发布。

安装

2tvenom/cborencode 添加到 composer.json 中作为需求

{
    "require": {
       "2tvenom/cborencode": "1.0.0"
    }
}

使用

<?php
include("vendor/autoload.php");

//target for encode
$target = array(true, array("variable1" => 100000, "variable2" => "Hello, World!", "Hello!"), 0.234, 0, null, 590834290589032580);

//encoded string
$encoded_data = \CBOR\CBOREncoder::encode($target);

//debug info output
$byte_arr = unpack("C*", $encoded_data);

echo "Byte hex map = " . implode(" ", array_map(function($byte){
        return "0x" . strtoupper(dechex($byte));
    }, $byte_arr)) . PHP_EOL;

echo "Byte dec map = " . implode(" ", $byte_arr) . PHP_EOL;

//decode
$decoded_variable = \CBOR\CBOREncoder::decode($encoded_data);
//output
var_dump($decoded_variable);

兼容性

使用 Ruby 扩展 在编码和解码中进行了检查

已知问题

  • 不支持标签。6 个主要类型 (未来可能支持)
  • 不支持 16 位和 32 位浮点编码 (可能在未来支持)
  • 所有浮点数将仅以 IEEE 754 双精度浮点数 (64 位) 进行序列化
  • 编码不支持不定长度值。