Guzzle - 获取原始 JSON 响应

Guzzle - Get Raw JSON Response

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

我正在尝试使用 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 中返回响应?可能吗? 我还尝试创建另一个要使用的模型,但类型要么是数组要么是对象,文档中的其余属性确实让我感到困惑,所以我尝试过的任何事情都无济于事。它似乎根本不应该通过模型或响应 Class,并且也许有某种方法可以绕过它("primitive" 对我来说有意义,但可惜没有) .

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

谢谢。

好的,所以我发现了一些对我有帮助的东西,即这个 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。