msgpack-php

msgpack.org[PHP]

  • Owner: msgpack/msgpack-php
  • Platform:
  • License:: BSD 3-Clause "New" or "Revised" License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

Msgpack for PHP

Build Status

This extension provide API for communicating with MessagePack serialization.

MessagePack is a binary-based efficient object serialization library.
It enables to exchange structured objects between many languages 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

Main metrics

Overview
Name With Ownermsgpack/msgpack-php
Primary LanguagePHP
Program languagePHP (Language Count: 4)
Platform
License:BSD 3-Clause "New" or "Revised" License
所有者活动
Created At2012-08-20 12:52:29
Pushed At2024-09-26 08:33:35
Last Commit At2024-09-26 09:10:21
Release Count19
Last Release Namemsgpack-3.0.0 (Posted on )
First Release Name0.5.2 (Posted on )
用户参与
Stargazers Count782
Watchers Count64
Fork Count118
Commits Count402
Has Issues Enabled
Issues Count122
Issue Open Count19
Pull Requests Count30
Pull Requests Open Count1
Pull Requests Close Count24
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private