Home >Backend Development >PHP Tutorial >Processing Protocol Buffers data in PHP
This article will explain issues related to processing Protocol Buffers data in PHP.
Protocol Buffers is a lightweight and efficient structured data storage format that can be used for structured data serialization and is very suitable for data storage or RPC data exchange format. It can be used as a language-independent, platform-independent, and extensible serialized structured data format in communication protocols, data storage and other fields. Currently, APIs in three languages: C, Java, and Python are provided.
Install protoc compiler
Download and install
$ wget https://github.com/google/protobuf/releases/download/v2.5.0/protobuf-2.5.0.tar.gz $ tar zxvf protobuf-2.5.0.tar.gz $ cd protobuf-2.5.0 $ ./configure --prefix=/usr/local/protobuf $ sudo make $ sudo make install
Installation verification:
$ /usr/local/protobuf/bin/protoc --version libprotoc 2.5.0
php extension
Install php extension
$ wget https://pecl.php.net/get/protocolbuffers-0.2.6.tgz $ tar zxvf protocolbuffers-0.2.6.tgz $ cd protocolbuffers-0.2.6 $ phpize $ ./configure $ sudo make $ sudo make install
Add in the php.ini configuration file: extension = "protocolbuffers.so"
$ php -m | grep protocolbuffers
protocolbuffers
protoc plug-in
$ mkdir ~/code/app$ cd ~/code/app$ composer require protocolbuffers/protoc-gen-php
Test:
Write proto file
$ vim demo.proto syntax = "proto2"; package Proto.Demo; message OrderInfo { required string name = 1 ; required int32 age = 2; required string amount = 3; } message UserInfo { required int32 uid = 1; required string address = 2; }
Generate php class library code
$ /usr/local/protobuf/bin/protoc --plugin=vendor/bin /protoc-gen-php --php_out=. -I. demo.proto
Write the test file:
$ vim demo.proto.php<?phpspl_autoload_register(function($name){ static $classmap; if (!$classmap) { $classmap = array( 'Proto\Demo\OrderInfo' => '/Proto/Demo/OrderInfo.php', 'Proto\Demo\UserInfo' => '/Proto/Demo/UserInfo.php', // @@protoc_insertion_point(autoloader_scope:classmap) ); } if (isset($classmap[$name])) { require __DIR__ . DIRECTORY_SEPARATOR . $classmap[$name]; } }); call_user_func(function(){ $registry = \ProtocolBuffers\ExtensionRegistry::getInstance(); // @@protoc_insertion_point(extension_scope:registry)}); $oi = new Proto\Demo\OrderInfo(); $oi->setName('Jack'); $oi->setAge(28); $oi->setAmount('500');//压缩数据$protoData = $oi->serializeToString(); var_dump("压缩数据:"); var_dump($protoData);//获取到$age的值$obj = Proto\Demo\OrderInfo::parseFromString($protoData); var_dump("获取数据:"); var_dump($obj->getName()); var_dump($obj->getAge()); var_dump($obj->getAmount());
Test
$ php demo.proto.php string(15) "压缩数据:"string(13) " Jack500"string(15) "获取数据:"string(4) "Jack"string(2) "28"string(3) "500"
This article explains PHP Related content about processing Protocol Buffers data. For more related knowledge, please pay attention to the PHP Chinese website.
Related recommendations:
##php sends XML data through curl and obtains XML data
PHP perfectly generates word documents, and html elements can be added
The above is the detailed content of Processing Protocol Buffers data in PHP. For more information, please follow other related articles on the PHP Chinese website!