Skip to content

msgpack/msgpack-php

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

* Fixed PHP-8.2 compatibility (see gh issue #165)

RC1:
* Fixed PHP-8.1 compatibility (see gh issues #161, #157, and #156)
* Added support for ZEND_ACC_NOT_SERIALIZABLE and magic __{,un}serialize
8568da4

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
January 14, 2021 13:45
November 20, 2019 13:36
January 14, 2021 13:45
October 7, 2022 10:20
November 20, 2019 22:42
August 17, 2021 11:19
January 14, 2021 13:36
December 20, 2019 09:49
July 17, 2010 18:46
January 14, 2021 13:36
June 12, 2015 16:35
November 30, 2020 17:38
December 27, 2010 11:09
October 7, 2022 10:15
November 20, 2019 13:36
October 7, 2022 11:04
October 7, 2022 11:04

Msgpack for PHP

Build Status

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:

pecl install msgpack

Compile Msgpack from source

/path/to/phpize
./configure --with-php-config=/path/to/php-config
make && make install

Example

<?php
$data = array(0 => 1, 1 => 2, 2 => 3);
$msg = msgpack_pack($data);
$data = msgpack_unpack($msg);

Advanced Example

<?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
$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