Using protobuffer in php

步履不停
Release: 2023-02-23 08:14:02
Original
4434 people have browsed it

Using protobuffer in php

Protobuf Introduction

protobuf (Protocol buffers) is a cross-platform, cross-language, scalable data transmission and storage protocol produced by Google. It is an efficient data compression One of the encoding methods.

Protocol buffers are flexible and efficient in serializing data. Compared to XML, Protocol buffers are smaller, faster, and simpler. Once the data structure of the data to be processed is defined, the code generation tool of Protocol buffers can be used to generate the relevant code. Data structures can even be updated without redeploying the program. Use Protobuf to describe your data structure once, and your structured data can be easily read and written in a variety of different languages ​​or from a variety of different data streams.

Protocol buffers are very suitable for data storage or RPC data exchange formats. A language-independent, platform-independent, and extensible serialized structured data format that can be used in communication protocols, data storage and other fields.

In addition, Protobuf is widely used in microservices due to its efficient data exchange efficiency on the intranet. Google's open source framework grpc is built based on this.

php-protobuf installation

Since protobuf does not natively support php, if php uses pb, you need to install the corresponding extension.

pecl install protobuf
Copy after login

The protoc compiler is required in the environment. Download and install method:

$ 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
Copy after login

Verify that the installation is successful:

$ /usr/local/protobuf/bin/protoc  --version
libprotoc 2.5.0
Copy after login

php-protobuf is installed successfully

php --ri protobuf
Copy after login

Installationlumenandgoogle/protobufDependencies

lumen new rpc
Copy after login
lumen new rpcThe command is equivalent to composer create-project laravel/lumen rpc
composer require google/protobuf
Copy after login

Add classmap under composer.json:

{
    "classmap": [
        "protobuf/"
    ]
}
Copy after login

ok, the preparations are all done.

Make a demo yourself

Create a protobuf folder in the code directorymkdir protobuf

Enter the directory and create a filesearchRequest .proto

syntax = "proto3";
message SearchRequest {
    string query = 1;
    int32 page_number = 2;
    int32 result_per_page = 3;
    enum Corpus {
        UNIVERSAL = 0;
        WEB = 1;
        IMAGES = 2;
        LOCAL = 3;
        NEWS = 4;
        PRODUCTS = 5;
        VIDEO = 6;
    }
    Corpus corpus = 4;
}
Copy after login

The above is the detailed content of Using protobuffer in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template