1. Go to this page and download the library: Download nullbio/cbor-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/ */
nullbio / cbor-php example snippets
use CBOR\Decoder;
use CBOR\MapObject;
use CBOR\TextStringObject;
use CBOR\UnsignedIntegerObject;
// Encoding: Create a CBOR object
$map = MapObject::create()
->add(TextStringObject::create('name'), TextStringObject::create('John Doe'))
->add(TextStringObject::create('age'), UnsignedIntegerObject::create(30));
$encoded = (string) $map;
// Decoding: Parse CBOR data
$decoder = Decoder::create();
$decoded = $decoder->decode(StringStream::create($encoded));
// Normalize to native PHP types
$data = $decoded->normalize();
// ['name' => 'John Doe', 'age' => '30']
use CBOR\UnsignedIntegerObject;
use CBOR\TextStringObject;
use CBOR\ListObject;
use CBOR\MapObject;
// Integers
$number = UnsignedIntegerObject::create(42);
// Strings
$text = TextStringObject::create('Hello World');
// Arrays
$list = ListObject::create([
UnsignedIntegerObject::create(1),
TextStringObject::create('two'),
]);
// Maps/Objects
$map = MapObject::create()
->add(TextStringObject::create('key'), TextStringObject::create('value'));
use CBOR\Tag\TimestampTag;
use CBOR\Tag\DecimalFractionTag;
use CBOR\UnsignedIntegerObject;
// Timestamps
$timestamp = TimestampTag::create(UnsignedIntegerObject::create(time()));
$dateTime = $timestamp->normalize(); // DateTimeImmutable
// Decimal fractions
$decimal = DecimalFractionTag::createFromFloat(3.14159);
echo $decimal->normalize(); // "3.14159"
use CBOR\Decoder;
use CBOR\MapObject;
use CBOR\ListObject;
use CBOR\TextStringObject;
use CBOR\UnsignedIntegerObject;
use CBOR\StringStream;
use CBOR\Tag\TimestampTag;
// Build a complex nested structure
$data = MapObject::create()
->add(
TextStringObject::create('user'),
MapObject::create()
->add(TextStringObject::create('name'), TextStringObject::create('Alice'))
->add(TextStringObject::create('age'), UnsignedIntegerObject::create(30))
)
->add(
TextStringObject::create('scores'),
ListObject::create([
UnsignedIntegerObject::create(95),
UnsignedIntegerObject::create(87),
])
)
->add(
TextStringObject::create('timestamp'),
TimestampTag::create(UnsignedIntegerObject::create(time()))
);
// Encode to binary
$encoded = (string) $data;
// Decode back
$decoder = Decoder::create();
$decoded = $decoder->decode(StringStream::create($encoded));
// Convert to native PHP types
$phpData = $decoded->normalize();
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.