_抱着西瓜过夏天 2021-07-12 14:34 采纳率: 0%
浏览 73

php如何在http传输中使用x-protobuf?

在对接的第三方文档中有明确指明:
1 通信协议为HTTP
2 序列化协议使用protobuf3,
3 HTTP header头Content-Type:application/x-protobuf。
于是按照文档的.proto文件执行,中间参考了一些资料:https://blog.csdn.net/u012129607/article/details/95758405 ,最后自己进行序列化和反序列化都能没有问题。想着应该是问题不大,但是在实际的请求中对方返回错误:“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($requestParam['os_type'] == 'android' ? 2 : 1);
        if ($requestParam['os_type'] == 'ios') {
            $index = \DeviceType::value('IDFA');
            $deviceId = $requestParam['imei'];
        } else {
            if (!empty($requestParam['imei'])) {
                $index = \DeviceType::value('IMEI');
                $deviceId = $requestParam['imei'];
            } elseif (!empty($requestParam['android_id'])) {
                $index = \DeviceType::value('ANDROID_ID');
                $deviceId = $requestParam['android_id'];
            } elseif (!empty($requestParam['oaid'])) {
                $index = \DeviceType::value('OAID');
                $deviceId = $requestParam['oaid'];
            } elseif (!empty($requestParam['mac'])) {
                $index = \DeviceType::value('MAC');
                $deviceId = $requestParam['mac'];
            } else {
                $index = \DeviceType::value('IMEI');
                $deviceId = $requestParam['imei'];
            }
        }
        $request->setDeviceId($deviceId);
        $request->setDeviceType($index);
        $pack = $request->serializeToString();

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

        $options = [
            'headers' => [
                'Content-Type' => 'application/x-protobuf',
            ],
            'json' => $pack
        ];
        $response = $this->request(self::$ad_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"}

难道,我这么使用理解上有问题吗还是?

  • 写回答

1条回答 默认 最新

  • Jason Ho 2021-07-12 17:06
    关注

    首先你这个头部加的格式有问题。
    正确的是:
    $header = array('Content-type: application/json', "ApiKey:vw:299222093deb4030b9c5db1307a7d1c44");

    评论

报告相同问题?

问题事件

  • 创建了问题 7月12日

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊