protobuffer php使用

protobuffer是google推出的一種數據傳的方式,具體壓縮,體積小的特色php

protobuffer自己不支持php,若要把.proto文件轉化爲php支持的文件,須要使用第三方的程序html

allegro/php-protobuf 或 drslump/Protobuf-PHPgit

 

參考 http://yueqian.sinaapp.com/a/52.htmlgithub

http://hello1010.com/php-protobufapp

 

使用drslump/Protobuf-PHPphp-fpm

一.下載並安裝protoc編譯器ui

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 installthis

二.下載並安裝protoc對應的php擴展google

https://github.com/chobie/php-protocolbuffersspa

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 = 1;
required int32 type = 2;
}

message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
repeated PhoneNumber phone = 4;
optional double 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

 /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

相關文章
相關標籤/搜索