php 如何在http中使用x-protobuf作为传输协议?

在对接的第三方文档中有明确指明:

1. 通信协议为HTTP
2. 序列化协议使用protobuf3,
3. HTTP请求头Content-Type:application/x-protobuf。

于是按照文档的.proto文件生成php代码,中间参考了一些资料:https://blog.csdn.net/u012129...,最后自己进行序列化和反序列化都能没有问题。想着应该是问题不大,但是在实际的请求中对方返回错误:“I/O error while reading input message; nested exception is com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.。 第一反应难道是我的使用方式不对?上代码:

        //序列化
        $request = new \Request();
        $request->setOs(2);
        $request->setDeviceId('869615046800475');
        $request->setDeviceType(0);
        $pack = $request->serializeToString();

        //反序列化
        $request = new \Request();
        $request->mergeFromString($pack);
        //var_dump($request->getOs(),  $request->getDeviceType(), $request->getDeviceId());
        //2, 0, 869615046800475

        $options = [
            'headers' => [
                'Content-Type' => 'application/x-protobuf',
            ],
            'json' => $pack
        ];
        $response = $this->request(self::$api, [], 'POST', $options);

响应结果:

{"timestamp":"2021-07-12T06:09:08.739+0000","status":400,"error":"Bad Request","message":"I/O error while reading input message; nested exception is com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length.","path":"/api/rta/silent/common"}

请教下大佬们,我这么使用理解上有问题吗还是哪里不对?

阅读 3.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题