https://github.com/protocolbuffers/protobuf/releases
當前下載protobuf2的最後一個版本
wget https://github.com/google/protobuf/archive/v2.6.1.zip unzip v2.6.1.zip -d ./ cd protobuf-2.6.1
請根據本身的需求下載對應的版本,當前操做演示版本爲protobuf2,及.proto文件中 syntax = "proto2";開頭的
sudo apt-get install autoconf automake libtool
其餘非debian發行版的機器安裝對應的依賴擴展便可
./autogen.sh
若是沒法鏈接google網站
修改php
curl http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2 | tar jx mv gtest-1.5.0 gtest
爲python
wget https://github.com/google/googletest/archive/release-1.5.0.tar.gz tar xzvf release-1.5.0.tar.gz mv googletest-release-1.5.0 gtest
也可由手動刪除google的鏈接,直接下載文件並解壓
./configure -h
./configure prefix=/opt/protobuf2
make && make install
權限不夠的話,要使用sudo
ln -s /opt/protobuf2/bin/protoc /usr/local/bin/protoc
創建軟鏈接,就能夠直接使用protoc命令了
protoc --proto_path=/home/zhangsan/protobuf --python_out=/home/zhangsan/python /home/zhangsan/protobuf/test.proto
參數proto_path指明.proto文件所在位置,若是不寫,會提示如下內容:linux
/home/zhangsan/protobuf/test.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
因此務必寫上.proto文件所在目錄路徑
python_out即.py生成的目錄git
最後一個參數即爲.proto文件的絕對路徑github
這行代碼若是使用IDE,提示無相關的類庫的話,直接刪掉/註釋便可數組
from google.protobuf import symbol_database as _symbol_database
生成的.py文件中,一下內容可能報類缺乏之類的問題,也可刪掉、註釋composer
_sym_db.RegisterMessage
文件中包含的空的數組 oneofs也可能給致使異常,刪掉、註釋便可curl
oneofs=[],
最後調整一下文件的代碼風格便可使用ide
request = DemoRequest() request.user.id = 1 request.user.name = 'zhangsan' body = request.SerializeToString() # body 既能夠使用http發送 response = http_func_name(body) message = response.getBody() message.ParseFromString() # message 即爲對應的數據對象 print(message.blog.id) print(message.blog.title)
https://github.com/allegro/php-protobuf/releases
wget https://github.com/allegro/php-protobuf/archive/v0.12.3.zip
unzip v0.12.3.zip -d ./
cd php-protobuf-0.12.3 phpize ./configure make make install
extension=protobuf.so
在擴展配置部分加入以上內容,執行 php -ir | grep protobuf輸出protobuf擴展信息即爲正常,如:
protobuf PWD => /home/qingliu/soft/php-protobuf-0.12.3 $_SERVER['PWD'] => /home/qingliu/soft/php-protobuf-0.12.3
composer install 安裝便可優化
protoc --proto_path=/home/zhangsan/protobuf --plugin=protoc-gen-allegrophp=protoc-gen-php.php --allegrophp_out=/home/zhangsan/php /home/zhangsan/protobuf/test.proto
請在allegro/php-protobuf的源碼目錄執行,正常執行完上述命令後,能夠在 /home/zhangsan/php目錄中看到生成的php文件,php無需修改,便可使用,如不符合本身的項目規範,能夠自行調節
使用這個composer包就行啦
composer require ruoge3s/protobuf-message-helper