Home  >  Article  >  Backend Development  >  Processing Protocol Buffers data in PHP

Processing Protocol Buffers data in PHP

jacklove
jackloveOriginal
2018-05-22 09:55:222423browse

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(      &#39;Proto\Demo\OrderInfo&#39; => &#39;/Proto/Demo/OrderInfo.php&#39;,      &#39;Proto\Demo\UserInfo&#39; => &#39;/Proto/Demo/UserInfo.php&#39;,      // @@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(&#39;Jack&#39;);
$oi->setAge(28);
$oi->setAmount(&#39;500&#39;);//压缩数据$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:

About PHP express query class

##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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn