2015-11-19 48 views
0

我一直試圖讓這一整天工作,所以這是我最後的手段。Gu - - 獲取原始JSON響應

我試圖用狂飲調用的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" 
     } 
     } 
    } 
    } 
} 

我有一個客戶,操作只是簡單地返回顧客的迴應zzle,例如:

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中得到響應?可能嗎? 我也試着創建另一個模型來使用,但類型是數組或對象,其餘的屬性在文檔中真的讓我感到困惑,所以我沒有嘗試過幫助。它似乎不應該完全通過模型或響應類,並且有可能繞過它(也許「原始」對我來說是有意義的,但是不是)。

順便說一句,我是新來的古怪,我知道這似乎有點過分爲這一個電話設計,但它對其他地方很重要,它的地方,我想我的腦袋圍繞它,如果有可能用它。

謝謝。

+0

這可能有幫助:http://guzzle3.readthedocs.org/http-client/response.html#response-body - 你應該能夠得到響應作爲一個字符串。 – Richard

+0

我已經閱讀了這些文檔,但我無法弄清楚我可以用它們做什麼,我們的實現已經包含在服務定義中,所以它看起來並不像我可以在那一刻攔截請求 –

+0

好吧我找到了一些額外的文檔,我會把它放在一個答案。謝謝 –

回答

0

行,所以我發現一對夫婦的事情,幫助我,也就是這個 https://github.com/Rarst/wporg-client/blob/33fb0dcce9f170b92824d09f5717ca814d7ecd29/php/WporgService.php

這是基於狂飲,所以我把參數從「身體」響應響應模型,並提出這樣的:

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

哪個返回了Guzzle流。因此搜索流的文檔,我發現這個 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。