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

相關文章
相關標籤/搜索