php安装 protobuf2.6.1扩展

首次实际项目中用到protobuf,找了下资料,弄了下环境并跑通,作下记录以避免忘记php

因为对接方使用的是proto2.4版本,因此也只能使用版本2的了。注意下,大版本间彷佛是不支持的,咱们试过安装版本3,但跟第三方用的版本2死活参数不匹配。(版本2官方没有提供php支持源码,因此找的是第三方支持的源码。版本3的官方已经提供了对php支持源码。)nginx

--------protobuf是什么--------git

官方解释:github

What are protocol buffers?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.composer

--------安装--------curl

一、protobuf安装:google

编译源码地址 -- https://github.com/protocolbuffers/protobuf/releases?after=v3.0.0-alpha-1url

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

tar zxvf protobuf-2.6.1.tar.gz

cd protobuf-2.6.1

./configure --prefix=/usr/local/protobuf

make && make install

#export PATH=/usr/local/protobuf/bin:$PATH //按默认的可不执行

//验证是否安装ok,显示相应版本号
protoc --version

二、php的protobuf扩展安装:spa

wget https://github.com/allegro/php-protobuf/archive/master.zip
 
unzip master.zip
 
cd php-protobuf-master
 
#yum install php-devel(安装依赖--可跳过)
 
phpize
 
./configure --with-php-config=/usr/local/php/bin/php-config 
 
make && make install
 
//将在/your folder/php-protobuf-master/module/目录下生成protobuf.so文件
//而后在php.ini里面加一下extension = "protobuf.so",再重启php与nginx便可。

三、composer--为了后面能够执行.proto文件自动生成代码code

//进入到php-protobuf-master目录
cd /your folder/php-protobuf-master

curl -s http://getcomposer.org/installer | php

php composer.phar install

--------protobuf使用--------

编译生成代码(一个message结构会生成一个包装类)

php ./php-protobuf-master/protoc-gen-php.php test_km.proto
//将会生成代码目录结构:/xxx/xxx/*

将生成的代码完整拷贝到本身的项目中

代码中类会继承ProtobufMessage类,它是protobuf.so中自动加载

------------------------------------------

这样就能够在项目里使用protobuf进行序列化/反序列化操做了

附:

protocol buffers官方:https://developers.google.com/protocol-buffers/?hl=zh-cn

protobuf github:https://github.com/protocolbuffers/protobuf

相关文章
相关标签/搜索