久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      PHP環(huán)境中使用ProtoBuf數(shù)據(jù)格式

      PHP環(huán)境中使用ProtoBuf數(shù)據(jù)格式

      前言

      ??RPC是google公司主導(dǎo)的一款RPC框架,并使用protobuf作為數(shù)據(jù)傳輸格式,伴隨gRPC框架的成熟及使用人群的增加,對于底層使用的數(shù)據(jù)格式protobuf也被越來越受到重視,而對于PHP生態(tài)而言,相關(guān)ProtoBuf介紹文檔及使用資料比較少,故此寫簡文希望能幫助到一些有需要的同學(xué)。

      ??ProtoBuf (Google Protocol Buffer)是由google公司用于數(shù)據(jù)交換的序列結(jié)構(gòu)化數(shù)據(jù)格式,具有跨平臺、跨語言、可擴(kuò)展特性,類型于常用的XML及JSON,但具有更小的傳輸體積、更高的編碼、解碼能力,特別適合于數(shù)據(jù)存儲、網(wǎng)絡(luò)數(shù)據(jù)傳輸?shù)葘Υ鎯w積、實(shí)時(shí)性要求高的領(lǐng)域。

      ??目前官方ProtoBuf 最新版本ProtoBuf3,已經(jīng)支持多種語言:C++C#GoJavaPythonRubyObject C JavascriptPHP,并且提供工具很方便地根據(jù)不同語言產(chǎn)生ProtoBuf需要的類庫。

      ??下面將通過Person數(shù)據(jù)格式作為示例進(jìn)行描述如果在PHP環(huán)境中如何使用ProtoBuf3。

      一、定義一個(gè)消息類型

      創(chuàng)建一個(gè)關(guān)于Person的定義文件(以.proto為后綴),如示例為person.proto,文件內(nèi)容如下:

      syntax="proto3"; package test; message Person{  string name=1;//姓名  int32 age=2;//年齡  bool sex=3;//性別 }

      1、syntax="proto3":表明使用的是proto3格式,如果不指定則為proto2

      2、package test:定義包名為test,生成類時(shí),會產(chǎn)生一個(gè)目錄為test

      3、message Person:消息主體內(nèi)容,里面為各個(gè)字段的定義

      二、生成對應(yīng)的PHP類

      定義好Person的格式后,該格式如果不生成我們所需要的類庫,其實(shí)是無任何意義的,還google提供一個(gè)工具protoc生成我們要的類庫。

      1、安裝protoc

      安裝地址:protobuf-php-3.5.1.tar.gz,目前最新為3.5.1

      官方發(fā)布地址:https://github.com/google/protobuf/releases/tag/v3.5.1

      解壓并安裝:

      tar -zxvf protobuf-php-3.5.1.tar.gz cd protobuf-3.5.1 ./configure --prefix=/opt/soft/protobuf make make install

      2、生成類庫

      /opt/soft/protobuf/bin/protoc –php_out=./ person.proto

      生成后將在當(dāng)前目錄產(chǎn)生如下文件:

      GPBMetadata/Person.php

      Test/Person.php

      三、在PHP中使用ProtoBuf

      在PHP中使用ProtoBuf依賴一個(gè)protobuf的擴(kuò)展,目前提供兩種方式進(jìn)行使用,1:php的c擴(kuò)展,2:php的lib擴(kuò)展包,這兩者均可在剛才下載包里可以找到。

      另外,也可以使用composer進(jìn)行安裝該依賴擴(kuò)展:composer require google/protobuf

      這里我主要是使用composer安裝,應(yīng)該它可以幫我產(chǎn)生autoload

      安裝好依賴后,我們就可以開始在php環(huán)境下使用protobuf了

      1、序列化

      <?php include 'vendor/autoload.php'; include 'GPBMetadata/Person.php'; include 'Test/Person.php';  $person = new TestPerson(); $person->setName("lailaiji"); $person->setAge("28"); $person->setSex(true); $data = $person->serializeToString(); file_put_contents('data.bin',$data);

      2、反序列化

      <?php include 'vendor/autoload.php'; include 'GPBMetadata/Person.php'; include 'Test/Person.php'; $bindata = file_get_contents('./data.bin'); $person = new TestPerson(); $person->mergeFromString($bindata); echo $person->getName();

      可以正常地輸出lailaiji

      PHP常用的使用方法:

      序列化:

      1、serializeToString:序列化成二進(jìn)制字符串

      2、serializeToJsonString:序列化成JSON字符串

      反序列化:

      1、mergeFromString:二進(jìn)制字符串反序列化

      2、mergeFromJsonString:Json字符串反序列化

      .proto的message解析

      1、定義:

      類型 變量名=位置;

      如:int32 age=1;

      這里需要區(qū)分,變量名后面的數(shù)字意義為該變量內(nèi)容在二進(jìn)制序列中的位置而不是變量的值,該數(shù)字必須是唯一不可重復(fù)使用。

      2、目前支持的類型:

      double,float,int32,int64,uint32 ,uint64,sint32,sint64

      fixed32,fixed64,sfixed32,sfixed64,bool,bytes

      推薦教程:《PHP》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號