Search code examples
phplaravel-5responseguzzle

How to access Response Body of GuzzleHttp and Extract Data of Response?


I'm creating application with Guzzle and Laravel 5.4. In there I'm doing request to external API and It gives response like this.

{
  "scope": "PRODUCTION",
  "token_type": "bearer",
  "expires_in": 3600,
  "refresh_token": "",
  "access_token": ""
}

And I need to access to the access_token property of this response. How I access these in GuzzleHttp. Response Content type is in application/json


Solution

  • I have resolved it with this method,

    $array = $response->getBody()->getContents();
    $json = json_decode($array, true);
    $collection = collect($json);
    $access = $collection->get('access_token');