ndunks/php-simple-protobuf

v1.2 2018-03-17 12:39 UTC

This package is not auto-updated.

Last update: 2024-09-26 05:39:06 UTC


README

PHP 实现的 Google Protocol Buffers

  • 不安装/编译 PHP 扩展的简单 Protobuf 实现
  • 支持不使用任何二进制/可执行文件编译 proto 2 文件
  • 支持组类型字段

关于 Protobuf

Protocol Buffers(通常称为 protobuf)是 Google 的语言中立、平台中立、可扩展的序列化结构化数据的机制。您可以在 Google 开发者网站上找到protobuf 的文档

入门指南

要求

  • PHP 5.6 或更高版本
  • php-json 扩展(用于 JSON 序列化器)

安装

使用 composer 安装。

# add ndunks/php-simple-protobuf entry to your composer.json
{
    "require": {
        "ndunks/php-simple-protobuf": "*"
    }
}

# install requirements composer
composer install

使用方法

编译 proto 2 文件到 PHP 类

只需在项目根目录的命令行中执行 PHP 代码

vendor\bin\compiler.php.bat --out=<out-dir> --file=<proto-file>

运行代码

include 'vendor/autoload.php';
// Class Simple compiled from simple.proto (find on test/proto)
$obj = new Simple();
$obj->setName('user');
$obj->setAddress('Indonesia');
$obj->setAge(25);
$obj->toArray(); // as PHP Array
$obj->toJson(); // as JSON formated string

Linux 上的示例

在 Linux 上设置和安装示例,请确保您的 php.ini(cli)中 short_open_tag 设置为 On

git clone https://github.com/ndunks/php-simple-protobuf
cd php-simple-protobuf
composer dump-autoload

如果您只想使用它(而不是开发它),可以跳过 composer install,因为此项目不依赖于任何其他 composer 库。运行以下命令编译示例 proto(在项目目录中)

# if output directory not exist, you must create it manualy
mkdir result
# Compile it
php bin/compiler.php --out=result --file=test/proto/simple.proto

检查结果目录,您将得到从 simple.proto 生成的 Simple.php 文件