protobuffer php,protobuffer php使用

protobuffer是google推出的一种数据传的方式,具体压缩,体积小的特点

protobuffer本身不支持php,若要把.proto文件转化为php支持的文件,需要使用第三方的程序

一.下载并安装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 {

requiredstring number = 1;

required int32 type= 2;

}

message Person {

requiredstring name = 1;

required int32 id= 2;

optionalstring email = 3;

repeated PhoneNumber phone= 4;

optionaldouble money = 5;

}

message AddressBook {

repeated Person person= 1;

}

生成proto对应的php文件

/usr/local/php5/bin/php  /home/source/aa/ab/cd/php-protobuf-master/protoc-php.php -t .  test2.proto

vi example_2.php

<?phprequire_once ‘pb_proto_test2.php‘;$foo = newPerson();$foo->setName(‘abc‘);$foo->setId(1);$foo->setEmail(‘abc‘);$foo->setMoney(321321.32);$phone_num = newPhoneNumber();$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 = newPerson();$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

原文:http://www.cnblogs.com/taek/p/5470693.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值