【问题标题】:Guzzle - Get Raw JSON ResponseGuzzle - 获取原始 JSON 响应
【发布时间】:2015-11-19 17:59:15
【问题描述】:

我整天都在努力让它工作,所以这是我最后的手段。

我正在尝试使用 guzzle 调用 api,以及这个 drupal_symfony_inject 模块: https://github.com/Capgemini/drupal_symfony_inject

我在 hook_symfony_yaml_config_params() 中有以下配置:

// Assets client.
$assets_resources_file = $resources_folder . '/moderation.json';
$param_array['bubl_api_assets_service_client.service.description'] = $assets_resources_file;
$param_array['bubl_api_assets_client.class'] = 'BUBL\Clients\Assets\Client';

这在我的服务定义中:

{
  "name": "BUBL moderation client",
  "apiVersion": "v1",
  "description": "BUBL moderation REST API client",
  "operations": {
    "getAllBubls": {
      "httpMethod": "GET",
      "uri": "/su/bubls",
      "responseModel": "getJSONResponse",
      "parameters": {
        "before": {
          "location": "uri",
          "type": "integer",
          "required": false
        },
        "after": {
          "location": "uri",
          "type": "integer",
          "required": false
        },
        "max": {
          "location": "uri",
          "type": "integer",
          "required": false
        }
      }
    }
  },
  "models": {
    "getJSONResponse": {
      "type": "object",
      "additionalProperties": {
        "location": "json"
      }
    },
    "getHTMLResponse": {
      "type": "object",
      "properties": {
        "html": {
          "location": "body",
          "type": "string"
        }
      }
    }
  }
}

我有一个客户端,其操作只是从 guzzle 返回响应,例如:

public function getAllBubls($before = NULL, $after = NULL) {
    $response = $this->client->sendRequest('getAllBubls', ['before' => $before, 'after' => $after]);
    return $response;
  }

当使用它时,它工作得非常好,就像这样:

global $_drupal_symfony_di_container;
    /** @var BUBL\Clients\Moderation\Client $client */
    $client = $_drupal_symfony_di_container->get('bubl_api_moderation_client');
    if (is_callable(array($client, $service_name))) {
      $response = $client->{$service_name}();
      return $data_source = $response['bubls'];
    }

现在,我的问题是响应是在我得到它之前通过 json_decode 运行的,并给了我一个数组,这导致了迁移模块的各种问题。我知道这是因为 responseModel 设置在

"responseModel": "getJSONResponse",

但是,我找不到简单地请求原始响应的方法。我已经尝试过(正如文档中提到的那样),完全删除 responseModal 并添加:

"responseType": "primitive",

和(我的意思是分开的)

"responseModel": "getHTMLResponse",

但不幸的是,我不会收到任何返回的数据 - 只是一个空数组。我似乎无法找到一种方法来忽略所有解析并仅以 JSON 格式获取响应?是否可以? 我还尝试创建另一个要使用的模型,但类型是数组或对象,文档中的其余属性对我来说真的很困惑,所以我尝试过的没有任何帮助。它似乎根本不应该通过模型或响应类,并且可能有某种方法可以绕过它(“原始”对我来说是有意义的,但可惜不是)。

顺便说一句,我是新手,我知道这对于这个电话来说似乎有点过度设计,但它对其他地方很重要,它已经到位,如果可以使用,我想了解一下它。

谢谢。

【问题讨论】:

  • 这可能会有所帮助:guzzle3.readthedocs.org/http-client/response.html#response-body - 您应该能够以字符串的形式获取响应。
  • 我已经阅读了这些文档,但我不知道我能用它们做什么,我们的实现包含在服务定义中,所以看起来我无法拦截那时的请求
  • 好的,我找到了一些额外的文档,我会把它放在答案中。谢谢

标签: json symfony drupal guzzle


【解决方案1】:

好的,所以我发现了一些对我有帮助的东西,即 https://github.com/Rarst/wporg-client/blob/33fb0dcce9f170b92824d09f5717ca814d7ecd29/php/WporgService.php

这是基于guzzle的,所以我从'body'响应模型中获取了参数并做了这个:

"getRawResponse": {
      "type": "object",
      "properties": {
        "body": {
          "location": "body",
          "type": "string"
        }
      }
    }

返回 Guzzle Stream。所以搜索流文档我发现了这个 Not getting expected response from Guzzle

这指示我将请求更改为

public function getAllBubls($before = NULL, $after = NULL) {
    $response = $this->client->sendRequest('getAllBubls', ['before' => $before, 'after' => $after]);
    // Get the raw JSON response.
    return $response['body']->getContents();
  }

它返回给我未解析的 JSON。

【讨论】:

  • 这不是解析前的 RAW HTTP 响应;但这是实现这一目标的公平方式。我来这里是为了寻找 RAW 和组合的标题和正文。我不想要低级传输细节,但低级 RAW 字节在某些情况下可能很有用。
猜你喜欢
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多