protobuffer php使用
protobuffer是google推出的一种数据传的方式,具体压缩,体积小的特点
protobuffer本身不支持php,若要把.proto文件转化为php支持的文件,需要使用第三方的程序
allegro/php-protobuf 或 drslump/Protobuf-PHP
参考 http://yueqian.sinaapp.com/a/52.html
http://hello1010.com/php-protobuf
一.下载并安装protoc编译器
tar -xzf protobuf-2.4.1.tar.gz && cd protobuf-2.4.1
cd protobuf-2.1.0
./configure --prefix=/usr/local/proto
make
make check
make install
二.下载并安装protoc对应的php扩展
https://github.com/chobie/php-protocolbuffers
wget --no-check-certificate https://github.com/chobie/php-protocolbuffers/archive/master.zip
unzip master && cd php-protocolbuffers-master/
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
make && make install
重启php
kill -USR2 `cat /usr/local/php5/var/run/php-fpm.pid`
生成的.so文件是protocolbuffers.so
allegro/php-protobuf 本身就带扩展,同时支持将.proto文件转为php文件
生成的.so文件是protobuf.so
三.下载 drslump/Protobuf-PHP 得到protoc-gen-php插件
并执行
/usr/local/php5/bin/pear channel-discover pear.pollinimini.net
/usr/local/php5/bin/pear install drslump/Protobuf-beta
四.将.proto文件转为php文件
/usr/local/proto/bin/protoc --plugin=protoc-gen-php='/home/source/Protobuf-PHP-master/protoc-gen-php.php' --proto_path='/home/source/Protobuf-PHP-master' --php_out=':./' '/home/source/Protobuf-PHP-master/test.proto'
执行后报错
PHP Warning: Declaration of google\protobuf\DescriptorProto::clearExtension() should be compatible with DrSlump\Protobuf\Message::clearExtension($extname) in /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 688
--php_out: protoc-gen-php: Plugin output is unparseable.
类中的方法重复了,注释掉它就行
PHP Warning: Missing argument 1 for DrSlump\Protobuf\Message::hasExtension(), called in /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Compiler/PhpGenerator.php on line 280 and defined in /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Message.php on line 283
PHP Notice: Undefined variable: extname in /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Message.php on line 285
PHP Warning: Missing argument 1 for DrSlump\Protobuf\Message::hasExtension(), called in /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Compiler/PhpGenerator.php on line 87 and defined in /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Message.php on line 283
PHP Notice: Undefined variable: extname in /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Message.php on line 285
--php_out: protoc-gen-php: Plugin output is unparseable.
library/DrSlump/Protobuf/Compiler/PhpGenerator.php
87行
if ($proto->hasExtension()) {
改为
if ($proto->hasExtension(null)) {
280行
if ($msg->hasExtension()) {
改为
if ($msg->hasExtension(null)) {
vi /home/source/Protobuf-PHP-master/library/DrSlump/Protobuf/Compiler/Cli.php
第二行添加
set_include_path('.:/usr/local/php5/share/pear/');
可执行文件了
使用allegro/php-protobuf 貌似这是个protoc的php客户端
wget https://github.com/allegro/php-protobuf/archive/master.zip
unzip master.zip && cd php-protobuf-master
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
make && make install
重启php
kill -USR2 `cat /usr/local/php5/var/run/php-fpm.pid`
使用方法
/usr/local/php5/bin/php /home/source/aa/ab/cd/php-protobuf-master/protoc-php.php -t . test2.proto
注意一定要加上 -t 表示输入文件的路径,不然没有文件被输出
例子:
vi test2.proto
message PhoneNumber {
required string number = ;
required int32 type = ;
}
message Person {
required string name = ;
required int32 id = ;
optional string email = ;
repeated PhoneNumber phone = ;
optional double money = ;
}
message AddressBook {
repeated Person person = ;
}
生成proto对应的php文件
/usr/local/php5/bin/php /home/source/aa/ab/cd/php-protobuf-master/protoc-php.php -t . test2.proto
/usr/local/php/bin/php -c /usr/local/php/lib/php.ini /home/source/php-7.0.2/ext/protobuf/protoc-php.php -t . test_3.proto
vi example_2.php
<?php
require_once 'pb_proto_test2.php'; $foo = new Person();
$foo->setName('abc');
$foo->setId(1);
$foo->setEmail('abc');
$foo->setMoney(321321.32); $phone_num = new PhoneNumber();
$phone_num->setNumber('16589875625');
$phone_num->setType(3); $foo->appendPhone($phone_num);
//$foo->appendPhone(2);
$packed = $foo->serializeToString();
//echo $packed;exit;
#$foo->clear();
echo "-----------src------------\n";
echo $foo->getName() ."\n";
echo $foo->getPhone()[0]->getNumber() ."\n";
$foo->dump();
echo "------------------------\n\n\n"; try {
$p = new Person();
$p->parseFromString($packed);
echo "------------parsed-------\n";
echo $p->getName() ."\n";
echo $p->getEmail() ."\n";
echo $p->getMoney() ."\n";
echo $p->getId() . "\n";
echo $p->getPhone()[0]->getNumber() ."\n"; //$p->dump();
echo "------------------------\n";
//print_r($xiao);
} catch (Exception $ex) {
die('Upss.. there is a bug in this example');
}
执行php文件
/usr/local/php5/bin/php example_2.php
protobuffer php使用的更多相关文章
- protobuffer序列化
一. 描述对象的proto文件 第一行package:对象经过protobuffer编译后形成java文件,这个文件放在按照package新建的文件夹内 java_package:java类的包名 j ...
- 为Cocos2d-x的Android平台加入Protobuffer支持
为Cocos2d-x的Android平台加入Protobuffer支持 分类: 工作2013-11-27 18:00 386人阅读 评论(1) 收藏 举报 cocos2d-xandroid平台交叉编译 ...
- Erlang使用ProtoBuffer
最近有工作需要打算为项目服务器做一个机器人,测试测试压力,根据自己的经验,使用Erlang来做是最合适不过的了,但是服务器使用的C++语言,使用了Google的ProtoBuffer作为协议进行数据交 ...
- 基于.NET CORE微服务框架 -surging 基于messagepack、protobuffer、json.net 性能对比
1.前言 surging内部使用的是高性能RPC远程服务调用,如果用json.net序列化肯定性能上达不到最优,所以后面扩展了protobuf,messagepack序列化组件,以支持RPC二进制传输 ...
- 【开源】AspnetCore 2.0 自动API文档生成组件,支持protobuffer
本文地址 http://www.cnblogs.com/likeli/p/8204054.html 关于 API文档自动生成,用于对APP端的开发帮助文档生成,默认ProtoBuffer传输格式. 本 ...
- 基于.NET CORE微服务框架 -谈谈surging 的messagepack、protobuffer、json.net 序列化
1.前言 surging内部使用的是高性能RPC远程服务调用,如果用json.net序列化肯定性能上达不到最优,所以后面扩展了protobuf,messagepack序列化组件,以支持RPC二进制传输 ...
- Python使用ProtoBuffer
Python使用ProtoBuffer Protocol Buffers,是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储.通信协议等方面. 就可读性而言感 ...
- Protobuffer简介c#
一.Protobuffer和json深度对比 JSON相信大家都知道是什么东西,如果不知道,那可就真的OUT了,GOOGLE一下去.这里就不介绍啥的了. Protobuffer大家估计就很少听说了,但 ...
- 编译gRPC Go版本使用的 ProtoBuffer 文件
本篇文章主要解决mac下安装ProtoBuffer,编译go版本gRPC用的.proto文件 安装 protoc 注意,gRPC 需要用到 proto3, 而目前 Release 的版本是 2.6.1 ...
随机推荐
- how to enable the Accessibility in the app
第一部分 先要装一个accchecker,全称是 UI Accessibility Checker .下载地址: http://acccheck.codeplex.com/ 装了之后 用这个工具可以 ...
- window.load 和$(document).ready() 、window.load和body onload区别
1.执行时间 window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行. $(document).ready()是DOM结构绘制完毕后就执行,不必等到加载完毕.2.编写个数不同 w ...
- 【Jsoup】Jsoup解析Html标签(Java后台解析)
中文API网站(下载地址): http://www.open-open.com/jsoup/ 有时候编辑器传到后台的内容是带Html标签的,或者有时候需要形成一个完整的Html文档,也或者需要解析其中 ...
- 2018.07.03 POJ 1279Art Gallery(半平面交)
Art Gallery Time Limit: 1000MS Memory Limit: 10000K Description The art galleries of the new and ver ...
- Linux服务器部署系列之三—DNS篇
网上介绍DNS的知识很多,在这里我就不再讲述DNS原理及做名词解释了.本篇我们将以一个实例为例来讲述DNS的配置,实验环境如下: 域名:guoxuemin.cn, 子域:shenzhen.guoxue ...
- org.eclipse.ui.PartInitException: Unable to open editor, unknown editor ID: org.xmen.ui.text.XMLTextEditor
无法打开struts模式的编译xml的编译器,然后打开.project文件,编辑最后一行,找到<natures>结点,增加一条<nature>com.genuitec.ecli ...
- ansible-playbook 主机变量2
ansible-playbook 配置 hosts 后可以指定变量,通过-k 可以交互输入密码,也可以将密码写在 hosts 文件中. 入口 yaml 文件中通过 {{ ** }} 获取变量,命令行通 ...
- Codeforces777A Shell Game 2017-05-04 17:11 59人阅读 评论(0) 收藏
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- HDU1175 连连看(bfs) 2016-07-24 13:27 115人阅读 评论(0) 收藏
连连看 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通 ...
- “一键GHOST”系统备份与还原(icmzn)
“一键GHOST”系统备份与还原(icmzn) 1.软件介绍 软件名称:一键GHOST 软件版本:v2014.01.14 安装环境:WINXP/2000/2003/WIN7/VISTA/2008/WI ...