呼叫未定義的方法 GuzzleHttp\Command\Result::getBody()
P粉426780515
P粉426780515 2023-08-29 22:08:59
0
1
520
<p>我試圖在 Drupal 中從外部 API 以 json 格式取得回應。我正在使用 HTTP 客戶端管理器 Drupal 模組。 現在我只能在陣列中獲得 stdClass 物件格式的回應,並且所有回應鍵值都遺失。 </p> <p>我的原始碼:</p> <pre class="brush:php;toolbar:false;">public function findPosts() { $client = $this->getClient(); $params = array('client_Id' => "12345", "client_Secret" => "42452454", "scope" => "read"); $response = $client->FindPosts($params); dpm($response); return ['#markup' => $response]; }</pre> <p>輸出以下程式碼。我也需要它看起來像 [access_token] => eyJhbGciOiJIUzUxMiIsIn,[type] => bearer 等。 </p> <pre class="brush:php;toolbar:false;">stdClass Object ( [__CLASS__] => GuzzleHttp\Command\Result [data:protected] => Array ( [0] => eyJhbGciOiJIUzUxMiIsIn [1] => bearer [2] => 3600 [3] => 2022-11-09T10:48:47 00:00 [4] => read [5] => MwA1ADkAZAA0AGIAZAA4AC0AOQAzADcA [6] => 86400 [7] => 2022-11-10T09:48:47 00:00 ) )</pre> <p>當我嘗試 $response->getBody() 或 $response->getContent() 或任何其他回應方法時,它會傳回以下錯誤。</p> <pre class="brush:php;toolbar:false;">錯誤:呼叫 Drupal\http_client_manager_example\Controller\ExampleController 中未定義的方法 GuzzleHttp\Command\Result::getBody()(第 92 行)模組/contrib /http_client_manager/modules/http_client_manager_example/src/Controller/ExampleController.php)。 Drupal\http_client_manager_example\Controller\ExampleController->findPosts() call_user_func_array(Array, Array) (第 123 行) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (行:564) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object)(行:124) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (第 97 行) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (行:169) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (行:81) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (第 58 行) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (行:48) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (行:106) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (行:85) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (行:49) Asm89\Stack\Cors->handle(Object, 1, 1) (第 48 行) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (行:38) Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (行:51) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (行:23) Stack\StackedHttpKernel->handle(Object, 1, 1) (行:709) Drupal\Core\DrupalKernel->handle(Object)(第 19 行)

P粉426780515
P粉426780515

全部回覆(1)
P粉231079976

正如 @bwaindwain 提到的 toArray() 方法代替 getBody() 起作用。

toArray() 方法適用於以下格式的回應:

[
    {
      "token": "4354iojegdjss"
    }
]

但是,使用這種回應格式,所有鍵仍然消失:

{
      "token": "4354iojegdjss"
 }

我對此問題的解決方法是在 src/api/resources/posts.json 中手動新增 responseModel,如下所示:

{
  "operations": {
    "GetToken": {
      "httpMethod": "POST",
      "uri": "token",
      "summary": "Get token",
      },
      "responseModel": "Token"
  }
}

 "models": {
    "Token": {
      "type": "object",
      "location": "json",
      "properties": {
        "token": {
          "location": "json",
          "type": "string"
        }
       }
    }
 }

如果有人知道更好的解決方案,請發表評論。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板