zoukankan      html  css  js  c++  java
  • protobuf的安装

    Protobuf的安装

        下面记录下在windows中如何安装protobuf,以及可能遇到的问题。

         1、下载

        下载所需的安装包,地址为:https://github.com/protocolbuffers/protobuf/releases

           

          下载完成之后解压

          

         2、将bin目录将入环境变量

         


        3、查看安装是否成功

        protoc  --version

     

        4、 新建一个文件:prototest/app/mail.proto

    1 // mail.proto
    2 
    3 syntax = 'proto3';
    4 package mail;
    5 message MailConfig{
    6   string to   = 1;
    7   string from = 2;
    8   string msg  = 3;
    9 }

       有一个地方需要注意,我这里使用的phpstorm现在是不支持proto文件的高亮显示。

       因此,为了方便开发,还需要设置:File->Settings->Plugins->安装Protobuf Support扩展

       

       5、执行命令生成PHP相关文件:protoc --php_out=. mail.proto

       –php_out=.表示编译成PHP代码,放在当前目录(.),也可以指定文件夹。 

        

        6、在PHP中使用ProtoBuf

        在PHP中使用ProtoBuf依赖一个protobuf的扩展,目前提供两种方式进行使用:

        1) php的c扩展

        2)php的lib扩展包,这两者均可在刚才下载包里可以找到。

       另外,也可以使用composer进行安装该依赖扩展:composer require google/protobuf

       这里我主要是使用composer安装,应该它可以帮助我们产生autoload。安装好依赖后,我们就可以开始在php环境下使用protobuf了

        安装中遇到的问题:

        执行:composer require google/protobuf

        报错:

        The "fxp/composer-asset-plugin" plugin (installed globally) was skipped because it requires a Plugin API version ("^1.0") that does not match your Composer installation ("2.1.0"). You may need to run composer update with the "--no-plugins" option.

        解决办法:将composer回退到版本1

        composer self-update --1

        

        7、使用composer安装以后

        在刚才创建的项目prototest目录下,会生成相应的vendor目录,如下图:

         

        8、在项目的根目录新建index.php,接下来我们来看下如何使用protoc。prototest/app/index.php 内容如下:

     1 <?php
     2 defined('DS') or define('DS',DIRECTORY_SEPARATOR);
     3 
     4 require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';
     5 require 'GPBMetadata/Mail.php';
     6 require 'Mail/MailConfig.php';
     7 
     8 // Write写数据
     9 $foo=new MailMailConfig();
    10 $foo->setTo("Tom");
    11 $foo->setFrom("John");
    12 $foo->setMsg("Don't forget the meeting!");
    13 $packed=$foo->serializeToString();
    14 
    15 // Reader读数据
    16 $res=new MailMailConfig();
    17 $res->mergeFromString($packed);
    18 $jsonArr=[
    19     "to"=>$res->getTo(),
    20     "from"=>$res->getFrom(),
    21     "msg"=>$res->getMsg(),
    22 ];
    23 
    24 var_dump($jsonArr);

      访问一下,得到结果如下:

      array(3) { ["to"]=> string(3) "Tom" ["from"]=> string(4) "John" ["msg"]=> string(25) "Don't forget the meeting!" }

      

      以上内容就是PHP简单使用protobuf的整个过程,后面我们再来熟悉下protobuf的语法。 

     

      参考链接:https://blog.csdn.net/wplblog/article/details/112537824

  • 相关阅读:
    ClickOnce發布經驗
    reporting Server組件不全引起的致命錯誤
    異步調用
    Usercontrol Hosted in IE
    MATLAB命令大全(转载)
    一种保护眼睛的好方法
    关于oracle自动编号
    An Algorithm Summary of Programming Collective Intelligence (1)
    An Algorithm Summary of Programming Collective Intelligence (3)
    An Algorithm Summary of Programming Collective Intelligence (4)
  • 原文地址:https://www.cnblogs.com/hld123/p/14942331.html
Copyright © 2011-2022 走看看