Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use protobuf in php

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to operate and use protobuf in php". In daily operation, I believe many people have doubts about how to operate and use protobuf in php. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to operate and use protobuf in php". Next, please follow the editor to study!

Summary

Using protobuf (v3) in PHP for serialization and deserialization, what steps need to be taken from installation to use, and what problems to pay attention to.

Operating environment

Centos7.8

Php7.4

Installation

Search google/protobuf on github, find the official source database documents, find the corresponding PHP documents, and follow the official documents.

The installation in the document involves two parts, one is to install the C extension for protobuf, and the other is the dependency package google/protobuf.

The C extension is installed using the pecl command (you need to use find /-name pecl to find it, and the installation path is not in the environment variable)

In addition, other commands need to be installed first, and the download and installation path of the C extension does not meet expectations (my php uses remi-php 's library installation, the path is different from the default setting of the original image)

After that, you need to establish a soft link to protobuf.so.

You can then install the dependency package using composer.

Next, you need the code generator protoc, [installation of proto], parse the definition file * .proto and generate the corresponding php code. For use in the project, you need to introduce the corresponding code. You need to change the composer.json file to add the autoload configuration. If you are testing, you can also manually require.

Use

There are two main points of use, one is serialization and the other is deserialization.

$pb=new Demo (); $pb- > setName ('demo'); / / serialization, data not visible $string=$pb- > serializeToString (); / serialization, data can see $string=$pb- > serializeToJsonString (); / / deserialization $pb=new Demo (); / / serialization value obtained from db, deserialization assignment to $pb, and then you can use $pb- > mergeFromString ($string); $pb- > mergeFromJsonString ($string); $pb- > getName () / / output demo here, the study on "how to operate and use protobuf in php" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report