本文整理汇总了PHP中msgpack_unpack函数的典型用法代码示例。如果您正苦于以下问题:PHP msgpack_unpack函数的具体用法?PHP msgpack_unpack怎么用?PHP msgpack_unpack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了msgpack_unpack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: decode
/**
* Decodes given value into the on-memory instance in accordance with
* the predefined codec.
*
* @param mixed $value value to be decoded.
* @return mixed instance which is decoded.
*/
public function decode($value)
{
if (function_exists('msgpack_unpack')) {
return msgpack_unpack($value);
}
return unserialize($value);
}
示例2: packData
public function packData($var, $bool = true, $option = array())
{
$option += array('packer' => $this->option['packer']);
switch ($option['packer']) {
case BoxConstants::ENCODER_JSON:
if ($bool == true) {
return json_encode($var);
} else {
return json_decode($var, true);
}
break;
case BoxConstants::ENCODER_MSGPACK:
if ($bool == true) {
return msgpack_pack($var);
} else {
return msgpack_unpack($var);
}
break;
case BoxConstants::ENCODER_SERIALIZE:
default:
if ($bool == true) {
return serialize($var);
} else {
return unserialize($var);
}
break;
}
}
示例3: parse
/**
* Parse Payload Data
*
* @param string $payload
*
* @throws ParserException
*
* @return array
*/
public function parse($payload)
{
if (function_exists('msgpack_unpack')) {
if ($payload) {
$prevHandler = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
throw new \Exception($errstr);
// @codeCoverageIgnore
});
try {
$msg = msgpack_unpack(trim($payload));
if (!$msg) {
throw new \Exception('Unknown error');
// @codeCoverageIgnore
}
} catch (\Exception $e) {
set_error_handler($prevHandler);
throw new ParserException('Failed To Parse MSGPack - ' . $e->getMessage());
}
set_error_handler($prevHandler);
return $msg;
}
return [];
}
throw new ParserException('Failed To Parse MSGPack - Supporting Library Not Available');
// @codeCoverageIgnore
}
示例4: decode
/**
* @param string $encoded
* @return mixed
* @throws \Exception
*/
public function decode($encoded)
{
if (function_exists('msgpack_unpack')) {
return msgpack_unpack(substr($encoded, 1));
}
throw new \Exception('msgpack not installed');
}
示例5: unserialize
public function unserialize($string)
{
if (!function_exists('msgpack_unpack')) {
throw new \Exception('msgpack extension must be installed!');
}
return msgpack_unpack($string);
}
示例6: get
/**
* Function pretreatment
* @param $_get
* @param $_post
* @param $_version
* @param $_scope
* @param $_interface
* @return array
*/
public static function get()
{
$_tmp_request = [];
//MAKE URL REQUEST
//MAKE HTTP HEADER REQUEST
$_tmp_header_request = [];
$_tmp_http_accept = explode(';', str_replace(' ', '', strtolower($_SERVER['HTTP_ACCEPT'])));
$_tmp_request['content-type'] = $_tmp_http_accept[0] == TYPE_JSON || $_tmp_http_accept[0] == TYPE_MSGPACK ? $_tmp_http_accept[0] : TYPE_NULL;
if (isset($_tmp_http_accept[1]) && !empty($_tmp_http_accept[1])) {
$_tmp = explode('=', $_tmp_http_accept[1]);
isset($_tmp[1]) && !empty($_tmp[1]) ? $_tmp_request['version'] = $_tmp[1] : FALSE;
}
//@todo Ranges
//MAKE CONTENT REQUEST
$_tmp_request['content'] = file_get_contents('php://input');
switch ($_tmp_request['content-type']) {
case TYPE_MSGPACK:
$_tmp_request['content'] = msgpack_unpack($_tmp_request['content']);
break;
case TYPE_JSON:
$_tmp_request['content'] = json_decode($_tmp_request['content']);
break;
case TYPE_NULL:
default:
$_tmp_request['content'] = NULL;
break;
}
return $_tmp_request;
}
示例7: handleRequest
public function handleRequest()
{
$args = func_get_args();
$data = $args[0];
//TODO 简单实用 msgpack
//FIXME 数据传输协议依赖swoole 底层组包功能,此处不在检查数据完整
$datastr = substr($data, 4);
$params = (object) msgpack_unpack($datastr);
if (empty($params->op)) {
$params->op = 'main.main';
}
//TODO 这里的处理应该完善点
list($ctrl, $method) = explode('.', $params->op);
$className = $this->getCtrlNamespace() . '\\' . ucfirst($ctrl) . 'Ctrl';
$ctrl = new $className();
$ctrl->setParams($params);
//发送msgpack编码数据,头部为数据长度
$result = $ctrl->{$method}();
if (Ping::$server->debug) {
echo "swoole handle request:\n";
echo "\tctrl: {$ctrl} method: {$method}\n";
echo "\tparams:\n\t\t" . json_encode($params) . "\n";
echo "\tresponse:\n\t\t" . json_encode($result) . "\n";
echo "\n\n";
}
$rawResult = msgpack_pack($result);
$rawResult = pack('N1', strlen($rawResult));
return $rawResult;
}
示例8: unpack
/**
* 解包工具
*
* @param string $raw
*
* @return mixed
*/
public static function unpack($raw, $prefix = true)
{
if ($prefix) {
return msgpack_unpack(substr($raw, 4));
} else {
return msgpack_unpack($raw);
}
}
示例9: unserialize
/**
* Unserializes the data
*
* @param array $data data to be unserialized
*
* @return array unserialized message
*/
public function unserialize($data)
{
$mssg = msgpack_unpack($data);
//lets just make UUIDs readable incase we need to debug
$mssg[0] = Helper::binToUuid($mssg[0]);
$mssg[1] = Helper::binToUuid($mssg[1]);
return $mssg;
}
示例10: onData
public function onData($data, Stream $conn)
{
$message = msgpack_unpack($data);
printf("Request method: %s\n", $message['method']);
printf("Request params: %s\n", print_r($message['params'], true));
$result = $this->handler->process($message);
$conn->write($result);
$conn->end();
}
示例11: testSend
public function testSend()
{
$io = m::mock(Io::class);
$io->shouldReceive('write')->andReturnUsing(function ($data) {
self::assertEquals(array(1, 2, 3), msgpack_unpack($data));
});
$messenger = new MsgpackMessenger($io);
$messenger->send(array(1, 2, 3));
}
示例12: unpack
/**
*/
public function unpack($data)
{
ini_set('track_errors', 1);
$out = @msgpack_unpack($data);
ini_restore('track_errors');
if (!isset($php_errormsg)) {
return $out;
}
throw new Horde_Pack_Exception('Error when unpacking Msgpack data.');
}
示例13: parse
public function parse($rawBody, $contentType)
{
if (!extension_loaded('msgpack')) {
throw new BadRequestHttpException('Msgpack is not supported in this app server');
}
$unpacked = @msgpack_unpack($rawBody);
if ($unpacked === null && $rawBody !== chr(0xc0)) {
throw new BadRequestHttpException('Invalid MsgPack data in request body');
}
return $unpacked;
}
示例14: test
function test($type, $variable, $test = null)
{
$serialized = msgpack_pack($variable);
$unserialized = msgpack_unpack($serialized);
var_dump($unserialized);
if (!is_bool($test)) {
echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL;
} else {
echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL;
}
}
示例15: test
function test($type, $variable, $object, $result = null)
{
$serialized = msgpack_pack($variable);
$unserialized = msgpack_unpack($serialized, $object);
var_dump($unserialized);
if ($result) {
echo $unserialized == $result ? 'OK' : 'ERROR', PHP_EOL;
} else {
echo 'SKIP', PHP_EOL;
}
}