pecl install protobuf
Person
的消息類型,如下:syntax = "proto3";
package tutorial;
<br>
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;
}
// 1. 定義一個Person對象
$person = new Person();
$person->setId(1);
$person->setName("demo");
$person->setEmail("[email protected]");
<br>
// 2. 使用protobuf序列化該對象
$bytes = $person->serializeToString();
<br>
// 3. 顯示序列化后的數據
echo "Encoded Data: " . bin2hex($bytes);
Encoded Data: 0a0464656d6f120174657374406578616d706c652e636f6d
// 1. 定義字節數組
$data = hex2bin("0a0464656d6f120174657374406578616d706c652e636f6d");
<br>
// 2. 使用protobuf反序列化該字節數組
$person = new Person();
$person->parseFromString($data);
<br>
// 3. 顯示反序列化后的數據
echo "Id: " . $person->getId() . "<br/>";
echo "Name: " . $person->getName() . "<br/>";
echo "Email: " . $person->getEmail() . "<br/>";
Id: 1 Name: demo Email: [email protected]
上一篇css導航條下載