I’m trying to test out gRPC in my Laravel application and I’m running into an error while sending the request.
PHP Details:
PHP 8.0.29 (cli) (built: Jun 8 2023 15:24:43) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.29, Copyright (c) Zend Technologies with Xdebug v3.2.1, Copyright (c) 2002-2023, by Derick Rethans with Zend OPcache v8.0.29, Copyright (c), by Zend Technologies
OS: Ubuntu 22.04.2 LTS
I’ve followed the steps outlined below
- Followed the steps in this link to install gRPC (Ubuntu) –
- Installed spiral/php-grpc using composer
- Created .proto file as follows:
syntax = "proto3";
package calculator;
service Calculator {
rpc Add (AddRequest) returns (AddResponse);
}
message AddRequest {
int32 a = 1;
int32 b = 2;
}
message AddResponse {
int32 result = 1;
}
- Compiled the proto to PHP using the command:
protoc --php_out=./app/Protos --grpc_out=./app/Protos --plugin=protoc-gen-grpc=/home/user/grpc/cmake/build/grpc_php_plugin proto/calculator.proto
When I run this command, the following files will be created: AddRequest.php, AddResponse.php, CalculatorClient.php, Calculator.php
The compiled Calculator.php is this:
<?php
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: proto/calculator.proto
namespace AppProtosGPBMetadataProto;
class Calculator
{
public static $is_initialized = false;
public static function initOnce() {
$pool = GoogleProtobufInternalDescriptorPool::getGeneratedPool();
if (static::$is_initialized == true) {
return;
}
$pool->internalAddGeneratedFile(
'
�
proto/calculator.proto
calculator""
AddRequest
a (
b ("
AddResponse
result (2D
Calculator6
Add.calculator.AddRequest.calculator.AddResponsebproto3'
, true);
static::$is_initialized = true;
}
}
As seen in the above code, the first parameter in the function internalAddGeneratedFile contains some unsupported (binary?) characters.
While creating a client and making the request, I encounter an error at that function that says ‘Failed to parse binary descriptor’.
I have tried compiling the same proto file in Python and managed to send requests correctly without no errors. It looks like there is some issue related to the protoc compiler for PHP, however I’m unable to identify the exact nature of the issue.
Please help me to understand why this occurs and how I can fix it.
2
Answers
It seems that your .proto file contains a UTF-8 BOM header. Can you use some editor or configuration in your editor that doesn’t save the header.
Related: What's the difference between UTF-8 and UTF-8 with BOM?
I’m running into the same problem. Responding to https://stackoverflow.com/a/77319184/2243798 here.
I’m using Symfony with php v.8.2 on Mac Pro 13.2.1 (22D68)
Using https://grpc.github.io/grpc/php/md_src_php__r_e_a_d_m_e.html as reference.
To build protoc plugin i ran:
which creatd the
grpc_php_plugin
after running:
protoc -I=proto/clients/salesforce.proto --plugin=protoc-gen-grpc={correct_path}/grpc/cmake/build/grpc_php_plugin --php_out=./php_out --grpc_out=./grpc_out
is what i ran after following install steps for protobuf and grpc for PHP.Everything appears to be generating properly.
I don’t think the issue is with the header of the php file itself, but rather the formatting of the data variable which is passed into the "internalAddGeneratedFile()" method.
In both VSCode and PHPStorm, the generated php file is in UTF-8 which is expected. I can reformat to UTFBom (or with Bom) but that creates a whole new set of encoding problems (which I would expect in this scenario).
I’ll update here if I learn anything.