调用未定义的方法 GuzzleHttp\Command\Result::getBody()
P粉426780515
P粉426780515 2023-08-29 22:08:59
[PHP讨论组]
<p>我试图在 Drupal 中从外部 API 以 json 格式获取响应。我正在使用 HTTP 客户端管理器 Drupal 模块。 现在我只能在数组中获得 stdClass 对象格式的响应,并且所有响应键值都丢失。</p> <p>我的原始代码:</p> <pre class="brush:php;toolbar:false;">public function findPosts() { $client = $this-&gt;getClient(); $params = array('client_Id' =&gt; &quot;12345&quot;, &quot;client_Secret&quot; =&gt; &quot;42452454&quot;, &quot;scope&quot; =&gt; &quot;read&quot;); $response = $client-&gt;FindPosts($params); dpm($response); return ['#markup' =&gt; $response]; }</pre> <p>输出以下代码。我也需要它看起来像 [access_token] => eyJhbGciOiJIUzUxMiIsIn,[type] => bearer 等。</p> <pre class="brush:php;toolbar:false;">stdClass Object ( [__CLASS__] =&gt; GuzzleHttp\Command\Result [data:protected] =&gt; Array ( [0] =&gt; eyJhbGciOiJIUzUxMiIsIn [1] =&gt; bearer [2] =&gt; 3600 [3] =&gt; 2022-11-09T10:48:47+00:00 [4] =&gt; read [5] =&gt; MwA1ADkAZAA0AGIAZAA4AC0AOQAzADcA [6] =&gt; 86400 [7] =&gt; 2022-11-10T09:48:47+00:00 ) )</pre> <p>当我尝试 $response->getBody() 或 $response->getContent() 或任何其他响应方法时,它会返回以下错误。</p> <pre class="brush:php;toolbar:false;">Error: Call to undefined method GuzzleHttp\Command\Result::getBody() in Drupal\http_client_manager_example\Controller\ExampleController-&gt;findPosts() (line 92 of modules/contrib/http_client_manager/modules/http_client_manager_example/src/Controller/ExampleController.php). Drupal\http_client_manager_example\Controller\ExampleController-&gt;findPosts() call_user_func_array(Array, Array) (Line: 123) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 564) Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 124) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;wrapControllerExecutionInRenderContext(Array, Array) (Line: 97) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 169) Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 81) Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 58) Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 48) Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 106) Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 85) Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 49) Asm89\Stack\Cors-&gt;handle(Object, 1, 1) (Line: 48) Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 38) Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware-&gt;handle(Object, 1, 1) (Line: 51) Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 709) Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)</pre></p>
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"
        }
       }
    }
 }

如果有人知道更好的解决方案,请发表评论。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号