longpan 发表于 2017-3-22 06:50:51

php与protoBuffer入门例子

  1,先创建一个test_my.proto 文件(.proto)定义数据对象类型
  如下:
  message Tree
{
   required string name = 1;
   required int32 id = 2;
   optional string height = 3;
}
  2,新建一个php运行编译上面的数据对象,使之生成pb_proto_test_my.php,里面生成上方的Tree一个类信息
  编译代码如下:
  require_once('../parser/pb_parser.php');

$test = new PBParser();
$test->parse('./test_my.proto');
var_dump('File parsing done!');
  3,新建一个test_my.pb(.pb)的文件用来序列化存储数据和读取数据
  存储数据和读取数据代码如下:
  // first include pb_message
require_once('../message/pb_message.php');

// now read it with the old file
// include the generated file
require_once('./pb_proto_test_my.php');

$string = file_get_contents('./test_my.pb');

// Just read it
$tree = new Tree();
$tree->set_name('2323');

$string = $tree->SerializeToString();

var_dump($tree->name());

file_put_contents('test_my.pb', $string);
页: [1]
查看完整版本: php与protoBuffer入门例子