Acquaintance protobuf and related usage of php

Foreword  

  Hello everyone, this is my first post in the blog Park, I wanted to try to open technology blog long time ago, but for various reasons there has been no implementation plan. Open blog just want to live their own work-related things simple record before been in use evernote record, can not be more people to share some information and communication, so I plan to open blog, some of the experiences to be recorded before the evernote share out we explore and learn together, tentatively scheduled for the week to update a blog post, there are a later time may be able to increase the frequency of updates, because they do not often write, so literary talent can be ignored, there is also hope the wrong place timely correction. Ado, I began following my first post

 

text

  This paper describes the combination of a number of related usage under php protobuf, first php I believe we have a better understanding, it is a computer programming language for web development is mainly interpreted high-level object-oriented, which stands for Hypertext Preprocessor (Hypertext Preprocessor), and now the latest stable version is 7.1. The following highlights at protobuf, protobuf is an open-source Google open source protocol for data exchange, whose main feature is cross-platform, cross-language, scalable, it is an open source protocol for serializing structured data. Expanding platform in the industry, the field of cross-language data exchange with most is the use of json, but compared to json, main features protobuf performance scalability, security, faster, here we take a detailed look php is how to combine the structure of data sequences protobuf

  1, first need to install the development unit protobuf above, proposed here on github clone down source compiler installation, as to the following steps:

    1)git clone https://github.com/protocolbuffers/protobuf.git

    Protobuf into the source directory to perform initialization file autogen.sh installation directory (Note: sometimes return wrong in the implementation process can not be performed, probably because automake not installed, the installation of automake in the implementation of the general on it)

    2)./authgen.sh

    After the initial installation directory you can see, the current directory has generated configure file, the next step is to install linux compile the following standard installation steps on it

    3)./configure

    4)make

    5)make install

    After completion of the above steps are completed protobuf local installation, the installation can be executed successfully if protoc --version see the currently installed version under

  2, the next step is to install php extensions of the protobuf

    1) execute the following command to obtain protobuf of php extensions source

      git clone https://github.com/allegro/php-protobuf.git

    2) After obtaining the finished into php-protobuf source directory, php extension follows the installation protobuf

      cd php-protobuf

      phpize

      make

      make install

    3) After the completion of the implementation of the above command, in the extended catalog will generate a protobuf.so php dynamic link library file, add extension = protobuf.so configuration in php configuration file, through php -m command to check whether the extension there are effective.

    4) Finally, perform composer install installation php-protobuf related dependencies in the list below php-protobuf

  3, after the completion of the above preparations, we can begin a formal php + protobuf development work

    1) First, we need to add protobuf file, we named foo.proto, reads as follows:

syntax = "proto2";
message Foo
{
    required int32 bar = 1;
    optional string baz = 2;
    repeated float spam = 3;
}

      Here we define a simple message type, which has three data types

    2) Next, we want to use protoc tool to convert the file into .proto php code command is as follows:

      php protoc-gen-php.php foo.proto

     After completion of the implementation of the above command will generate a Foo.php file in the current directory, this is what we defined earlier Foo's php class file after file directly because this class we can operate a class Foo

    3) Finally, we add a file to use testFoo.php Foo class code is as follows:

require_once 'Foo.php';

$foo = new Foo();
$foo->setBar('string');
$foo->setBaz(1);
$foo->appendSpam(3.0);
$foo->appendSpam(4.0);

$packed = $foo->serializeToString();

$parsedFoo = new Foo();
$parsedFoo->clearSpam();
try {
    $parsedFoo->parseFromString($packed);
} catch (Exception $ex) {
    die('Oops.. there is a bug in this example, ' . $ex->getMessage());
}

$parsedFoo->dump();

    We will print the following after the execution of this file:

Foo {
  1: bar => 0
  2: baz => "1"
  3: spam(2) => 
    [0] => 3
    [1] => 4
}

    So far we have completed a basic php + protobuf development work, after about protobuf more detail again.

    Thank you, there is something wrong hope a lot of correction

  

Guess you like

Origin www.cnblogs.com/dunleavy/p/10964255.html