0%

protobuf扩展安装和使用

php protobuf 扩展支持(Docker)

protobuf安装

  1. 获取压缩包

    1
    wget https://github.com/protocolbuffers/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
  2. 解压

    1
    tar zxvf protobuf-2.6.1.tar.gz
  3. 编译

    1
    2
    cd protobuf-2.6.1
    ./configure --prefix=/usr/local/protobuf
  4. make 安装

    1
    make && make install
  5. 加入到环境变量

    1
    export PATH=/usr/local/protobuf/bin:$PATH
  6. 验证是否安装ok,显示相应版本号

    1
    protoc --version

php protobuf扩展安装

  1. 获取压缩包

    1
    wget https://github.com/allegro/php-protobuf/archive/master.zip
  2. 解压

    1
    unzip master.zip
  3. 使用phpize对php进行添加扩展

    1
    phpize
  4. 编译

    1
    ./configure --with-php-config=/www/server/php/71/bin/php-config
  5. make 安装

    1
    make && make install
  6. 编辑php.ini文件,添加一行

    1
    echo 'extension=protobuf.so' >> /www/server/php/71/etc/php.ini
  7. 重启php与nginx

composer

  1. 安装php-protobuf
    1
    2
    3
    cd /${PATH}/php-protobuf-master
    curl -s http://getcomposer.org/installer | php
    php composer.phar install

修改源代码

  1. 打开 PhpGenerator.php

    1
    2
    cd /${PATH}/php-protobuf-master
    vim src/Allegro/Protobuf/Compiler/PhpGenerator.php
  2. 修改 _createFileComment 函数,注释一行代码

    1
    //$comment->append('Auto generated from ' . basename($file->getName()) . ' at ' . $date);
  3. 修改 _describeSingleField 函数,在$comment = new CommentStringBuffer(self::TAB, self::EOL);之前设置 $returnCast 为空字符串(约 687 行)

    1
    2
    $returnCast = '';//新增的一行
    $comment = new CommentStringBuffer(self::TAB, self::EOL);

protobuf使用

  1. 编译生成代码到指定目录
    1
    php /root/php-protobuf-master/protoc-gen-php.php -o protobuf_msg/ miqu.proto

命令行选项

  • -o out, 生成文件的目标目录(默认为当前目录).
  • -I proto_path, 要在其中搜索导入的目录。
  • –protoc=protoc 原编译器的可执行路径。

执行编译