chobie / php-protocolbuffers

PECL ProtocolBuffers
pecl.php.net/package/protocolbuffers
Other
128 stars 38 forks source link

please help me.thanks #11

Closed dofounds closed 11 years ago

dofounds commented 11 years ago

so: <?php require "addressbook.proto.php"; $db = "addressbook.db"; $handle = fopen($db, "r"); $data = fread($handle, filesize ($db)); $person = new Tutorial_AddressBook (); $data = $person->parseFromString($data);

//$tm = $person->getDescriptor();

//echo $tm->getName(); print_r($data);

exit;

how to get the "$data" information. and use the class functions

chobie commented 11 years ago

Hi, parseFromString is static method. so we should use it like this.

<?php
require "php/addressbook.proto.php";
$db = "addressbook.db";
$handle = fopen($db, "r");
// $data is raw protocol buffers binary data. 
$data = fread($handle, filesize ($db));

// you can decode it with `ProtocolBuffers::decode("Tutorial_AddressBook", $data);`
$addressbook = ProtocolBuffers::decode("Tutorial_AddressBook", $data);
// or you can use $addressbook = Tutorial_AddressBook::parseFromString($data);
foreach ($addressbook->getPerson() as $person) {
  var_dump($person);
}

Thanks,

dofounds commented 11 years ago

Thank you very much.