php需要grpc扩展 下载地址
composer 需要安装两个包
grpc/grpc
google/protobuf
使用protoc 生成对应的php protocol buffers 文件
protoc --php_out=. token.proto
下面基于phalcon框架写的一个简单grpc调用
grpc 客户端代码
namespace AppUtils;
use Grpc;
class TokenClient extends GrpcBaseStub
{
public function __construct(string $hostname, array $opts, $channel = null)
{
parent::__construct($hostname, $opts, $channel);
}
public function GetComponentToken(ProtocReqTokenInfo $args)
{
//"ProtocReqTokenInfo" //路径为请求体对应的那个类
//"protoc.TokenService/GetComponentToken" //路径为proto文件对应的(包名.服务名/方法名)
return $this->_simpleRequest('/protoc.TokenService/GetComponentToken',
$args,
['ProtocResTokenInfo','decode']);
//"ProtocResTokenInfo" //路径为响应体对应的那个类
}
}
grpc php调用客户端实现代码
namespace ControllersProto;
use AppUtilsTokenClient;
use GrpcChannelCredentials;
use ProtocReqTokenInfo;
class Request
{
public function index()
{
$client = new TokenClient('10.0.1.62:7666',[
'credentials' => ChannelCredentials::createInsecure()
]);
$request = new ReqTokenInfo();
$request->setComponentAppid("wx9ad710182d38d107");
$get = $client->GetComponentToken($request)->wait();
var_dump($get);
}
}