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 App\Protos\GPBMetadata\Proto;
class Calculator
{
public static $is_initialized = false;
public static function initOnce() {
$pool = \Google\Protobuf\Internal\DescriptorPool::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.
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?