1. Go to this page and download the library: Download cnik87/msgpack2 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/ */
cnik87 / msgpack2 example snippets
use MessagePack\Packer;
$packer = new Packer();
...
$packed = $packer->pack($value);
use MessagePack\MessagePack;
...
$packed = MessagePack::pack($value);
use MessagePack\Packer;
use MessagePack\PackOptions;
// cast PHP strings to MP strings, PHP arrays to MP maps
// and PHP 64-bit floats (doubles) to MP 32-bit floats
$packer = new Packer(PackOptions::FORCE_STR | PackOptions::FORCE_MAP | PackOptions::FORCE_FLOAT32);
// cast PHP strings to MP binaries and PHP arrays to MP arrays
$packer = new Packer(PackOptions::FORCE_BIN | PackOptions::FORCE_ARR);
// these will throw MessagePack\Exception\InvalidOptionException
$packer = new Packer(PackOptions::FORCE_STR | PackOptions::FORCE_BIN);
$packer = new Packer(PackOptions::FORCE_FLOAT32 | PackOptions::FORCE_FLOAT64);
use MessagePack\BufferUnpacker;
$unpacker = new BufferUnpacker();
...
$unpacker->reset($packed);
$value = $unpacker->unpack();
use MessagePack\MessagePack;
...
$value = MessagePack::unpack($packed);
while ($chunk = ...) {
$unpacker->append($chunk);
if ($messages = $unpacker->tryUnpack()) {
return $messages;
}
}
namespace MessagePack\TypeTransformer;
use MessagePack\Packer;
use MessagePack\Type\Map;
class MapTransformer implements Packable
{
public function pack(Packer $packer, $value)
{
return $value instanceof Map
? $packer->packMap($value->map)
: null;
}
}
use MessagePack\Packer;
use MessagePack\PackOptions;
use MessagePack\Type\Map;
use MessagePack\TypeTransformer\MapTransformer;
$packer = new Packer(PackOptions::FORCE_ARR);
$packer->registerTransformer(new MapTransformer());
$packed = $packer->pack([
[1, 2, 3], // MP array
new Map([1, 2, 3]), // MP map
]);
use MessagePack\BufferUnpacker;
use MessagePack\Packer;
use MessagePack\TypeTransformer\Extension;
class DateTimeTransformer implements Extension
{
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function getType()
{
return $this->type;
}
public function pack(Packer $packer, $value)
{
if (!$value instanceof \DateTimeInterface && !$value instanceof \DateTime) {
return null;
}
return $packer->packExt($this->type,
$packer->packStr($value->format(\DateTime::RFC3339))
);
}
public function unpack(BufferUnpacker $unpacker, $extLength)
{
return new \DateTime($unpacker->unpackStr());
}
}
use App\MessagePack\DateTimeTransformer;
use MessagePack\BufferUnpacker;
use MessagePack\Packer;
$transformer = new DateTimeTransformer(42);
$packer = new Packer();
$packer->registerTransformer($transformer);
$unpacker = new BufferUnpacker();
$unpacker->registerTransformer($transformer);
$packed = $packer->pack(new DateTime());
$date = $unpacker->reset($packed)->unpack();
sh
$ php -n tests/bench.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.