我们这里采用的是扩展安装protobuf的方式,也有protobuf php库加载方式,引入官方写好的类库直接调用,这里是protobuf扩展中的c调用。
官方地址为:https://github.com/protocolbuffers/protobuf/tree/master/php
环境要求:
c扩展库需要php>5.5
php类库需要php>5.5
安装c的protobuf扩展,需要准备以下依赖工具:
- autoconf
- automake
- libtool
- make
- gcc
- pear
- pecl
例如在ubuntu下的安装方式为:
sudo apt-get install -y php-pear php5-dev autoconf automake libtool make gcc
下载protobuf库,切换到PHP目录,运行下面命令:
cd ext/google/protobuf
pear package
sudo pecl install protobuf-{VERSION}.tgz
当然也可以直接编译安装。
在当前ext/google/protobuf目录下执行下面命令:
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
这里的--with-php-config指向了php命令同一级目录的php-config执行文件。
如果没有报错,最终make install生成的.so扩展库会放到扩展库目录,命令最终行会显示路径,其后,只需要在php.ini中加载,并重启PHP即可使用。