Undefined method for encode/decode
I'm trying to learn about Protobuf in PHP using https://github.com/google/protobuf/tree/master/php . Currently I'm stuck in an error.
My steps to install protobuf:
Install protobuf through pecl with command:
sudo pecl install protobuf-3.2.0a1
Set composer.json as below, then run sudo composer install
{
"require": {
"google/protobuf": "^3.2"
}
}
Below is my code:
Proto file:
syntax = "proto3";
message APIReq {
string functionName = 1;
string name = 2;
int32 time = 3;
string type = 4;
}
Command to generate PHP Class from .proto file:
protoc --php_out=/var/www/html/ MsgFormat.proto
The protoc command resulted in two file, APIReq.php and GPBMetadata/MsgFormat.php
After that, I added require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/autoload.php';
and require_once __DIR__ . '/GPBMetadata/MsgFormat.php';
require_once __DIR__ . '/GPBMetadata/MsgFormat.php';
in the generated PHP file because when I ran php APIReq.php
it came up with
PHP Fatal error: Class 'GoogleProtobufInternalMessage' not found in /var/www/html/testing/APIReq.php on line 13
After I added those line, the error disappeared, so I assume both line fixed the problem
my PHP file (following example from https://developers.google.com/protocol-buffers/docs/reference/php-generated, section Messages):
<?php
require __DIR__ . '/vendor/autoload.php';
include_once('APIReq.php');
$param = new APIReq();
$param2 = new APIReq();
$param->setFunctionname('functionname');
$param->setName('name');
$param->setTime(123456);
$param->setType('type');
$dt = $param->encode();
$param2->decode($dt);
?>
When I run the PHP code, it returns error message:
PHP Fatal error: Call to undefined method APIReq::encode()
How can I fix this?
Edit: Tried this with protobuf 3.3.0 as well, with same result.
Encode & Decode not exist in the codebase as I traced down.
This change was introduced in 3.3.0
//to encode message
$data = $param->serializeToString();
//to decode message
$param2 = new APIReq();
$param2->mergeFromString($data);
链接地址: http://www.djcxy.com/p/64796.html
下一篇: 未定义的编码/解码方法