Maison  >  Questions et réponses  >  le corps du texte

Appel d'une méthode non définie GuzzleHttp\Command\Result::getBody()

<p>J'essaie d'obtenir une réponse au format json à partir d'une API externe dans Drupal. J'utilise le module Drupal HTTP Client Manager. Désormais, je ne peux obtenir la réponse qu'au format objet stdClass dans un tableau et toutes les valeurs des clés de réponse sont perdues. </p> <p>Mon code d'origine : </p> <pre class="brush:php;toolbar:false;">public function findPosts() { $client = $this->getClient(); $params = array('client_Id' => "12345", "client_Secret" => "portée" => "lire"); $response = $client->FindPosts($params); dpm($réponse); return ['#markup' => $response]; }</pré> <p>Sortez le code suivant. J'ai aussi besoin qu'il ressemble à [access_token] => eyJhbGciOiJIUzUxMiIsIn, [type] => Bearer etc. </p> <pre class="brush:php;toolbar:false;">Objet stdClass ( [__CLASS__] => [data:protected] => Tableau ( [0] =>eyJhbGciOiJIUzUxMiIsIn [1] => [2] => 3600 [3] =>2022-11-09T10:48:47+00:00 [4] => lire [5] =>MwA1ADkAZAA0AGIAZAA4AC0AOQAzADcA [6] => 86400 [7] =>2022-11-10T09:48:47+00:00 ) )</pré> <p>Lorsque j'essaie $response->getBody() ou $response->getContent() ou toute autre méthode de réponse, l'erreur suivante est renvoyée.</p> <pre class="brush:php;toolbar:false;">Erreur : appel à la méthode non définie GuzzleHttpCommandResult::getBody() dans Drupalhttp_client_manager_exampleControllerExampleController->findPosts() (ligne 92 de modules/contrib/http_client_manager/modules/http_client_manager_example /src/Controller/ExampleController.php). Drupalhttp_client_manager_exampleControllerExampleController->findPosts() call_user_func_array(Array, Array) (Ligne : 123) DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() (Ligne : 564) DrupalCoreRenderRenderer->executeInRenderContext(Object, Object) (Ligne : 124) DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Ligne : 97) DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() (Ligne : 169) SymfonyComponentHttpKernelHttpKernel->handleRaw(Object, 1) (Ligne : 81) SymfonyComponentHttpKernelHttpKernel->handle(Object, 1, 1) (Ligne : 58) DrupalCoreStackMiddlewareSession->handle(Object, 1, 1) (Ligne : 48) DrupalCoreStackMiddlewareKernelPreHandle->handle(Object, 1, 1) (Ligne : 106) Drupalpage_cacheStackMiddlewarePageCache->pass(Object, 1, 1) (Ligne : 85) Drupalpage_cacheStackMiddlewarePageCache->handle(Object, 1, 1) (Ligne : 49) Asm89StackCors->handle(Object, 1, 1) (Ligne : 48) DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object, 1, 1) (Ligne : 38) DrupalwebprofilerStackMiddlewareWebprofilerMiddleware->handle(Object, 1, 1) (Ligne : 51) DrupalCoreStackMiddlewareNegociationMiddleware->handle(Object, 1, 1) (Ligne : 23) StackStackedHttpKernel->handle(Object, 1, 1) (Ligne : 709) DrupalCoreDrupalKernel->handle(Object) (Ligne : 19)</pre></p>
P粉426780515P粉426780515387 Il y a quelques jours482

répondre à tous(1)je répondrai

  • P粉231079976

    P粉2310799762023-08-30 18:59:05

    Comme @bwaindwain mentionné toArray() 方法代替 getBody() fonctionne.

    La méthode

    toArray() fonctionne avec des réponses aux formats suivants :

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

    Cependant, avec ce format de réponse, toutes les clés disparaissent toujours :

    {
          "token": "4354iojegdjss"
     }

    Ma solution à ce problème se trouve dans src/api/resources/posts.json 中手动添加 responseModel comme indiqué ci-dessous :

    {
      "operations": {
        "GetToken": {
          "httpMethod": "POST",
          "uri": "token",
          "summary": "Get token",
          },
          "responseModel": "Token"
      }
    }
    
     "models": {
        "Token": {
          "type": "object",
          "location": "json",
          "properties": {
            "token": {
              "location": "json",
              "type": "string"
            }
           }
        }
     }

    Si quelqu'un connaît une meilleure solution, veuillez laisser un commentaire.

    répondre
    0
  • Annulerrépondre