php7.0使用protobuf,Protobuf在PHP上的安装和使用

郦祺
2023-12-01

参考 `https://github.com/protobuf-php/protobuf`

### 安装Protobuf扩展

到PECL上下载protobuf代码,编译安装

下载地址:https://pecl.php.net/package/protobuf

```

[root@api ~]# mkdir package

[root@api ~]# cd package

[root@api package]# wget https://pecl.php.net/get/protobuf-3.7.0.tgz

[root@api package]# tar xvf protobuf-3.7.0.tgz

[root@api package]# cd protobuf-3.7.0

[root@api protobuf-3.7.0]# phpize

[root@api protobuf-3.7.0]# ./configure

[root@api protobuf-3.7.0]# make && make install

```

### 安装Protobuf编译器

```

[root@api addressbook]# yum install protobuf-compiler

```

然后修改php.ini启用 protobuf扩展,重启php-fpm。用命令行`php -m`或者用`phpinfo()`函数查看扩展是否已经启用。

### 创建测试项目

我们把测试项目创建在`/data/code/addressbook`中

```

[root@api protobuf-3.7.0]# mkdir -p /data/code/addressbook

[root@api protobuf-3.7.0]# cd /data/code/addressbook

[root@api addressbook]# composer require protobuf-php/protobuf

[root@api addressbook]# vim composer.json

{

"require": {

"protobuf-php/protobuf": "^0.1.3"

},

"autoload": {

"psr-4": {

"Tutorial\\": "src/Tutorial/"

}

}

}

[root@api addressbook]# mkdir src

[root@api addressbook]# composer require "protobuf-php/protobuf-plugin"

[root@api addressbook]# vim addressbook.proto

```

addressbook.proto写入如下内容:

```

package tutorial;

import "php.proto";

option (php.package) = "Tutorial.AddressBookProtos";

message Person {

required string name = 1;

required int32 id = 2;

optional string email = 3;

enum PhoneType {

MOBILE = 0;

HOME = 1;

WORK = 2;

}

message PhoneNumber {

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

message AddressBook {

repeated Person person = 1;

}

```

开始编译,生成PHP类库

```

[root@api addressbook]# php ./vendor/bin/protobuf --include-descriptors -i . -o ./src/ ./addressbook.proto

PHP classes successfully generate.

```

创建命令程序

```

[root@api addressbook]# touch addressbook

[root@api addressbook]# chmod +x addressbook

[root@api addressbook]# vim addressbook

```

写入如下内容:

```

#!/usr/bin/env php

include 'vendor/autoload.php';

use Tutorial\AddressBookProtos\Person;

use Tutorial\AddressBookProtos\AddressBook;

// Read the existing address book or create a new one.

$addressBook = is_file($argv[1])

? new AddressBook(file_get_contents($argv[1]))

: new AddressBook();

$person = new Person();

$id = intval(readline("Enter person ID: "));

$name = trim(readline("Enter person name: "));

$email = trim(readline("Enter email address (blank for none): "));

$person->setId($id);

$person->setName($name);

if ( ! empty($email)) {

$person->setEmail($email);

}

while (true) {

$number = trim(readline("Enter a phone number (or leave blank to finish):"));

if (empty($number)) {

break;

}

$phone = new Person\PhoneNumber();

$type = trim(readline("Is this a mobile, home, or work phone? "));

switch (strtolower($type)) {

case 'mobile':

$phone->setType(Person\PhoneType::MOBILE());

break;

case 'work':

$phone->setType(Person\PhoneType::WORK());

break;

case 'home':

$phone->setType(Person\PhoneType::HOME());

break;

default:

echo "Unknown phone type. Using default." . PHP_EOL;

}

$phone->setNumber($number);

$person->addPhone($phone);

}

// Add a person.

$addressBook->addPerson($person);

// Print current address book

echo $addressBook;

// Write the new address book back to disk.

file_put_contents($argv[1], $addressBook->toStream());

```

程序创建完毕,测试执行:

```

[root@api protobuf]# ./addressbook address.book

Enter person ID: 1

Enter person name: Yang Wenjie

Enter email address (blank for none): 13923458160

Enter a phone number (or leave blank to finish):0755-88889999

Is this a mobile, home, or work phone? home

[root@api protobuf]# ./addressbook address.book

Enter person ID: 1

Enter person name: Yang Wenjie

Enter email address (blank for none): 8162782@qq.com

Enter a phone number (or leave blank to finish):13923458160

Is this a mobile, home, or work phone? mobile

Enter a phone number (or leave blank to finish):0755-99998888

Is this a mobile, home, or work phone? work

Enter a phone number (or leave blank to finish):0755-99990000

Is this a mobile, home, or work phone? home

Enter a phone number (or leave blank to finish):

person {

name: "Yang Wenjie"

id: 1

email: "8162782@qq.com"

phone {

number: "13923458160"

type: MOBILE

}

phone {

number: "0755-99998888"

type: WORK

}

phone {

number: "0755-99990000"

type: HOME

}

}

[root@api protobuf]#

```

程序在当前目录下输出了address.book二进制文件保存数据。

 类似资料: