1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
# Msgpack for PHP
[](https://github.com/msgpack/msgpack-php/actions?query=workflow%3Aci+branch%3Amaster)
This extension provides an API for communicating with MessagePack serialization.
MessagePack is a binary-based efficient object serialization library.
It enables to exchange structured objects between many languages just like JSON.
But unlike JSON, it is very fast and small.
## Requirement
- PHP 7.0 +
## Install
### Install from PECL
Msgpack is an PECL extension, thus you can simply install it by:
```shell
pecl install msgpack
```
### Compile Msgpack from source
```shell
/path/to/phpize
./configure --with-php-config=/path/to/php-config
make && make install
```
### Example
```php
<?php
$data = array(0 => 1, 1 => 2, 2 => 3);
$msg = msgpack_pack($data);
$data = msgpack_unpack($msg);
```
### Advanced Example
```php
<?php
$data = array(0 => 1, 1 => 2, 2 => 3);
$packer = new \MessagePack(false);
// ^ same as $packer->setOption(\MessagePack::OPT_PHPONLY, false);
$packed = $packer->pack($data);
$unpacker = new \MessagePackUnpacker(false);
// ^ same as $unpacker->setOption(\MessagePack::OPT_PHPONLY, false);
$unpacker->feed($packed);
$unpacker->execute();
$unpacked = $unpacker->data();
$unpacker->reset();
```
### Advanced Streaming Example
```php
<?php
$data1 = array(0 => 1, 1 => 2, 2 => 3);
$data2 = array("a" => 1, "b" => 2, "c" => 3);
$packer = new \MessagePack(false);
$packed1 = $packer->pack($data1);
$packed2 = $packer->pack($data2);
$unpacker = new \MessagePackUnpacker(false);
$buffer = "";
$nread = 0;
//Simulating streaming data :)
$buffer .= $packed1;
$buffer .= $packed2;
while(true) {
if($unpacker->execute($buffer, $nread)) {
$msg = $unpacker->data();
var_dump($msg);
$unpacker->reset();
$buffer = substr($buffer, $nread);
$nread = 0;
if(!empty($buffer)) {
continue;
}
}
break;
}
```
## Resources
* [msgpack](http://msgpack.org/)
|