Protobuffer PHP Usage

Source: Internet
Author: User
Tags pear

Protobuffer is a data transmission by Google, specific compression, small size features

Protobuffer itself does not support PHP, to convert. proto files to PHP-supported files, you need to use a third-party program

Allegro/php-protobuf or drslump/protobuf-php

Reference http://yueqian.sinaapp.com/a/52.html

Http://hello1010.com/php-protobuf

Using drslump/protobuf-php

I. Download and install the PROTOC compiler

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

Two. Download and install the PHP extension for Protoc

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

Restart PHP

KILL-USR2 ' Cat/usr/local/php5/var/run/php-fpm.pid '

The resulting. So file is protocolbuffers.so

allegro/php-protobuf itself with extensions, while supporting the conversion of. proto Files to php files

The resulting. So file is protobuf.so

Three. Download drslump/protobuf-php get protoc-gen-php Plugin

and perform

/usr/local/php5/bin/pear Channel-discover pear.pollinimini.net
/usr/local/php5/bin/pear Install Drslump/protobuf-beta

Four. Convert. proto files to php files

/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 '

Error after execution

PHP warning:declaration of Google\protobuf\descriptorproto::clearextension () should is 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.

The method in the class is repeated, just comment it out.

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 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 rows

if ($proto->hasextension ()) {

Switch

if ($proto->hasextension (null)) {

280 rows

if ($msg->hasextension ()) {

Switch

if ($msg->hasextension (null)) {

vi/home/source/protobuf-php-master/library/drslump/protobuf/compiler/cli.php

The second line adds

Set_include_path ('.:/ Usr/local/php5/share/pear/');

Executable file.

Using allegro/php-protobuf looks like this is a protoc PHP client.

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

Restart PHP

KILL-USR2 ' Cat/usr/local/php5/var/run/php-fpm.pid '

How to use

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

Note that you must add-t to the path of the input file, otherwise no files are output

Example:

VI Test2.proto

message PhoneNumber {requiredstringNumber =1; required Int32 type=2;} Message person {requiredstringName =1; required Int32 ID=2; optionalstringemail =3; repeated PhoneNumber phone=4; optionalDoubleMoney =5;} Message AddressBook {repeated person person=1;}

Generate PHP files for proto

/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 ');}

Execute PHP file

/usr/local/php5/bin/php example_2.php

Protobuffer PHP Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.